I made a wrapper class for Ghostscript that works with framework 3.5, but not with framework 4.5.1.
I am using VB.NET 2013.
The Initialization code is:
If fails when it is called with this code.
This is the error I am receiving.
-----
PInvokeStackImbalance occurred
Message: Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\TGroff.PSI\Documents\Visual Studio 2013\Projects\SandBox\TestPdfThumbnails\TestPdfThumbnails\bin\x86\Debug\TestPdfThumbnails.vshost.exe '.
Additional information: A call to PInvoke function 'TestPdfThumbnails!TestPdfThumbnails.clsPDF::CopyMemory' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
-----
What do I do to make this work in framework 4.5.1?
I am using VB.NET 2013.
The Initialization code is:
Code:
' Copyright (c) 2002 Dan Mount and Ghostgum Software Pty Ltd
'
' Permission is hereby granted, free of charge, to any person obtaining
' a copy of this software and associated documentation files (the
' "Software"), to deal in the Software without restriction, including
' without limitation the rights to use, copy, modify, merge, publish,
' distribute, sublicense, and/or sell copies of the Software, and to
' permit persons to whom the Software is furnished to do so, subject to
' the following conditions:
'
' The above copyright notice and this permission notice shall be
' included in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
' BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
' ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
' CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
Option Strict Off
Option Explicit Off
Imports System.Runtime.InteropServices
Public Class clsPDF
'------------------------------------------------
'UDTs Start
'------------------------------------------------
<StructLayout(LayoutKind.Sequential)> Public Structure GS_Revision
Public strProduct As IntPtr
Public strCopyright As IntPtr
Public intRevision As Integer
Public intRevisionDate As Integer
End Structure
'------------------------------------------------
'UDTs End
'------------------------------------------------
'------------------------------------------------
'API Calls Start
'------------------------------------------------
'Win32 API
'GhostScript API
<DllImportAttribute("kernel32", entrypoint:="RtlMoveMemory")> _
Private Shared Sub CopyMemory(ByVal dest As IntPtr, ByVal source As IntPtr, ByVal bytes As Long)
End Sub
'...
Code:
Public Function gsdll_stdout(ByVal intGSInstanceHandle As IntPtr, ByVal strz As IntPtr, ByVal intBytes As Integer) As Integer
' If you can think of a more efficient method, please tell me!
' We need to convert from a byte buffer to a string
' First we create a byte array of the appropriate size
Dim aByte(intBytes) As Byte
' Then we get the address of the byte array
Dim gcByte As GCHandle = GCHandle.Alloc(aByte, GCHandleType.Pinned)
Dim ptrByte As IntPtr = gcByte.AddrOfPinnedObject()
' Then we copy the buffer to the byte array
CopyMemory(ptrByte, strz, intBytes) ' <=========== HERE
' Release the address locking
ptrByte = IntPtr.Zero
gcByte.Free()
' Then we copy the byte array to a string, character by character
Dim str As String = ""
Dim i As Integer
For i = 0 To intBytes - 1
str = str + Chr(aByte(i))
Next
' Finally we output the message
Console.Write(str)
gsdll_stdout = intBytes
End Function
-----
PInvokeStackImbalance occurred
Message: Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\TGroff.PSI\Documents\Visual Studio 2013\Projects\SandBox\TestPdfThumbnails\TestPdfThumbnails\bin\x86\Debug\TestPdfThumbnails.vshost.exe '.
Additional information: A call to PInvoke function 'TestPdfThumbnails!TestPdfThumbnails.clsPDF::CopyMemory' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
-----
What do I do to make this work in framework 4.5.1?