VB.NET pointer interop question - vb.net

I am trying to write a VB.NET program that will call a function in an unmanaged C DLL passing the structure like this:
typedef struct {
unsigned char *msg;
int msglen;
}
What I have not been able to figure out is how to handle the "unsigned char *msg" part. How would you define this in the VB.NET Structure?

<StructLayout(LayoutKind.Sequential)> _
public structure foo
<MarshalAs(UnmanagedType.LPStr)> dim msg as string
dim msgLen as integer
end structure

This depends a lot on how the memory for the msg field is handled. You need to be careful to free any allocated memory which is transfered to managed code.
That being said I think the most straight forward interop type is as follows
Public Structure S1
Public msg as IntPtr
Public msgLen as Integer
End Structure
To get the actual msg value as a String you'll need to use the following code.
Public Function GetString(ByVal s1 as S1) As String
return Marshal.PtrToStringAnsi(s1.msg, s1.msgLen)
End Function
To create an S1 instance based on a String do the following. Note: You will need to free the memory allocated here if the calling function does not take ownership.
Public Function CreateS1(ByVal str As String) As S1
Dim local As New S1
local.msg = Marshal.StringToHGlobalAnsi(str)
local.msgLen = str.Length
return local
End Function

Related

Can't call VB.NET COM DLL function with String from VBA

I implemented a VB.NET DLL with a simple test function:
<ComVisible(True)>
Function TestString ( <MarshalAs(UnmanagedType.BStr)> xyz As String) As <MarshalAs(UnmanagedType.BStr)> String
Dim y As Integer
TestString = "Hello"
End Function
The function is dead simple. In VBA I declare the function appropriately:
Public Declare Function TestString Lib "myDLL.dll" (xyz As String) As String
From VBA I also obviously load the DLL. However the issue is that when I run the function like so:
Dim st as String
st = "Hello"
Debug.Print TestString(ByVal st)
I get a message saying bad DLL calling convention. On the other hand when I remove the <MarshalAs(UnmanagedType.BStr)> the function works BUT crashes shortly after printing "Hello".
What am I doing wrong?
Public Declare Function is the syntax used to call functions exported from a DLL, this is a different mechanism from COM.
To use COM add a reference to the DLL, create an instance of the class containing TestString then call across that instance: r = classInstance.TestString("Hello")

Send String Msg between 2 .net applications (VB.NET)

I want to send a string Message from .net Application A and Receive the message from the Application B and here's the code:
-------- Application A
Private Const RF_TESTMESSAGE As Integer = &HA123
Public Structure MyData
Public M As String
Public I As Integer
End Structure
Public Function SendTest()
Dim Data As New MyData
Data.M = "QWERTY"
Data.I = 15
Dim P As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Data))
Marshal.StructureToPtr(Data, P, False)
Dim Hdl As New IntPtr(11111) 'While 11111 is the WndHD for the application B for testing
SendMessage(Hdl, RF_TESTMESSAGE, IntPtr.Zero, P)
End Function
------- Application B
Private Const RF_TESTMESSAGE As Integer = &HA123
Public Structure MyData
Public M As String
Public I As Integer
End Structure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = RF_TESTMESSAGE Then
Dim A = DirectCast(m.GetLParam(GetType(MyData)), MyData)
MsgBox(A.M)
MsgBox(A.I)
Marshal.FreeHGlobal(m.LParam)
End If
MyBase.WndProc(m)
End Sub
The application B always receive the message, but unable to convert the point lParam to valid MyData Structure, and sometime raise access violation, sometime no error ....
Please advice.
The problem is that you're not properly marshalling the data between the two applications. You are allocating memory in one application, and then passing a pointer to that memory to a second application. But because application's have private memory spaces and cannot read each other's memory, that pointer is useless to the second application.
Depending on what it points to in that application's memory space, it might be triggering an access violation, or just not working properly.
Perhaps you're confused by the naming of the AllocHGlobal and FreeHGlobal functions. Contrary to what first impressions might suggest, they do not actually allocate and free global memory. At least not memory that is globally accessible to all processes running on a machine. The name comes from the Windows HGLOBAL data type, which used to mean exactly this back in the days of 16-bit Windows, where all applications did share a common memory space and could read each others' memory. But that is no longer the case in modern 32-bit Windows. The names were retained for backwards compatibility reasons. HGLOBAL and HLOCAL mean effectively the same thing nowadays. More on the nitty gritty details is available on MSDN. But that's mostly a curiosity. You don't need to know and understand all of it to get the code working correctly.
The point is that all AllocHGlobal does is allocate memory from the process's default heap, readable only to that process. Hence the need to marshal the memory across processes, making it accessible from the other one receiving the message. Doing this manually is, of course, an option. But not a very good one. It's tricky to get right, and there's little point. Like Tim's comment hints at, the easier option is to use the WM_COPYDATA message, which does the marshalling for you. When you use this message, the data you want to share is packaged in a COPYDATASTRUCT structure.
You can keep most of your existing code to allocate memory, you just need to replace your custom RF_TESTMESSAGE window message with WM_COPYDATA. Sample code, including the necessary structure definition, is available over on the pinvoke website.
Something like this (warning—untested and uncompiled):
Private Const WM_COPYDATA As Integer = &H004A
<StructLayout(LayoutKind.Sequential)> _
Public Structure COPYDATASTRUCT
Public dwData As IntPtr
Public cdData As Integer
Public lpData As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure MyData
Public M As String
Public I As Integer
End Structure
Public Function SendTest()
' Create your data structure, MyData, and fill it.
Dim data As New MyData
data.M = "QWERTY"
data.I = 15
Dim pData As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(data))
Marshal.StructureToPtr(data, pData, False)
' Create the COPYDATASTRUCT you'll use to shuttle the data.
Dim copy As New COPYDATASTRUCT
copy.dwData = IntPtr.Zero
copy.lpData = pData
copy.cbData = Marshal.SizeOf(data)
Dim pCopy As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(copy))
Marshal.StructureToPtr(copy, pCopy, False)
' Send the message to the other application.
SendMessage(New IntPtr(11111), WM_COPYDATA, IntPtr.Zero, pCopy)
' Free the memory we allocated
' (This works because SendMessage is synchronous, and does not
' return until the other application has finished processing
' the data that you have sent it. That also means that the
' other application should not and cannot free the memory.
' If it needs it after processing the message, it needs to
' make a local copy.)
Marshal.FreeHGlobal(pCopy)
Marshal.FreeHGlobal(pData)
End Function
If you decide not to go the easy route using WM_COPYDATA and instead marshal the data yourself, you need to make sure to call the RegisterWindowMessage function (if you are not doing so already in code I can't see) to ensure that the ID of your custom window message is unique.

Name 'VarPtr' is not declared.In old vb code

I have a old code in VB.Now I convert it into vb.net.There is a line in a code
Dim pCParameters As Integer
pCParameters = VarPtr(Parameters)
When I execute code the error occure that
Name 'VarPtr' is not declared.
VarPtr not supported in vb.net.So how I replace it.
This is not as straight forward because your variables in .NET are managed. To do exactly what you are asking you need to look at GCHandle.Alloc and pin the variable so it cannot be moved. Then you can get its memory address.
Something like this (from memory):
GCHandle handle = GCHandle.Alloc(pCParameters , Pinned )
IntPtr ptr = handle.AddressOfPinnedObject
Yes I found the answer.The new VarPtr function is
Public Function VarPtr(ByVal e As Object) As Integer
Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return GC2

Can I define an assignment operator in vb.net Structure?

I am having a structure that is actually a simple byte with more functionality.
I defined it this way:
Structure DeltaTime
Private m_DeltaTime As Byte
Public ReadOnly DeltaTime As Byte
Get
Return m_DeltaTime
End Get
End Property
End Structure
I want to have these two functionalities:
Public Sub Main
Dim x As DeltaTime = 80 'Create a new instance of DeltaTime set to 80
Dim y As New ClassWithDtProperty With { .DeltaTime = 80 }
End Sub
Is there a way to achieve this?
If there would be a way to inherit from Structure I would simply inherit from Byte adding my functionality, basically I just need a byte Structure with custom functionality.
My question is also valid when you want to define your new singleton member value-types (like, you want to define a nibble-type for instance etc.) and you want to be able to set it with an assignment to number or other language typed representation.
In other words, I want to be able to do define the following Int4 (nibble) stucture and use it as follows:
Dim myNibble As Int4 = &HF 'Unsigned
Create a conversion operator, e.g.
Structure DeltaTime
Private m_DeltaTime As Byte
Public ReadOnly Property DeltaTime() As Byte
Get
Return m_DeltaTime
End Get
End Property
Public Shared Widening Operator CType(ByVal value As Byte) As DeltaTime
Return New DeltaTime With {.m_DeltaTime = value}
End Operator
End Structure
UPDATE:
For your proposed Int4 type I strongly suggest that you make it a Narrowing operator instead. This forces the user of your code to cast explicitly, which is a visual hint that the assignment might fail at runtime, e.g.
Dim x As Int4 = CType(&HF, Int4) ' should succeed
Dim y As Int4 = CType(&HFF, Int4) ' should fail with an OverflowException

How do I import and call unmanaged C dll with ANSI C string "char *" pointer string from VB.NET?

I have written my own function, which in C would be declared like this, using standard Win32 calling conventions:
int Thing( char * command, char * buffer, int * BufSize);
I have the following amount of Visual Basic code figured out, which should import the DLL file and call this function, wrapping it up to make it easy to call Thing("CommandHere",GetDataBackHere).
UPDATE: This code is now a working solution, as shown here:
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports System
Imports System.Text
Namespace dllInvocationSpace
Public Class dllInvoker
' I tried attributes, but I could not make it build:
' <DllImport("thing1.dll", False, CallingConvention.Cdecl, CharSet.Ansi, "Thing", True, True, False, True)>
Declare Ansi Function Thing Lib "thing1.dll" (ByVal Command As String, ByRef Buffer As StringBuilder, ByRef BufferLength As Integer) As Integer
' This part contributed by helpful user:
Shared Function dllCall(ByVal Command As String, ByRef Results As String) As Integer
Dim Buffer As StringBuilder = New StringBuilder(65536)
Dim Length As Integer = Buffer.Capacity
Dim retCode As Integer = Thing(Command, Buffer, Length)
Results = Buffer.ToString()
'Debug.Assert(Results.Length = Length) ' This assertion is not true for me
Return retCode
End Function
End Class
End Namespace
I got the code to build by following the help received here, and then I had forgot the As Return Type (which got me a MarshalDirectiveException PInvokeRestriction). Then I had an assertion failure inside my DLL, which lead to an SEHException. Once fixed, this works BEAUTIFULLY. Thank you folks. There are newsgroups where people are saying this can not be done, that Visual Basic only loads managed DLL assemblies (which I guess is the normal thing most Visual Basic users are used to).
It depends on how you use the buffer argument in your C code. If you only pass a string from your VB.NET code to your C code then declaring it ByVal String is good enough. If however you let the C code return a string in the buffer then you have to declare it ByVal StringBuilder and initialize it properly before the call. For example:
Public Class dllInvoker
Declare Ansi Function Thing Lib "Thing1.dll" (ByVal Command As String, ByVal Buffer As StringBuilder, ByRef BufferLength As Integer) As Integer
Shared Function dllCall(ByVal Command As String, ByRef Results As String) As Integer
Dim Buffer As StringBuilder = New StringBuilder(65536)
Dim Length As Integer = Buffer.Capacity
Dim retCode As Integer = Thing(Command, Buffer, Length)
Results = Buffer.ToString()
Debug.Assert(Results.Length = Length)
Return retCode
End Function
End Class
Note the ambiguity in the returned Length value.
You cannot convert a StringBuilder instance to a string instance, instead, use the 'ToString' method to convert it back to the string type...here's the portion of the code in the dllCall function...
retCode = Thing(Command, Buffer, bufsz)
Results = Buffer.ToString();