charliee1151
Member
- Joined
- Oct 22, 2013
- Messages
- 6
- Programming Experience
- 10+
I need to call a legacy DLL.
Here is the definition of one of the functions (abbreviated):
I change it to VB.net:
Here is VB.net code:
This works, but I barely understand why.
-------------------------------------------------
Here is second function, similar to first (abbreviated):
I change it to VB.net:
Here is VB.net code:
This does not work! The DLL returns an error indicating that the call has a bad parameter(s). I tried to follow the example of the
first (working) call. Is "Char" vs "Byte" different? How do I make call work with BYTE pointer? Or is it another problem?
Thank you.
Charlie
Here is the definition of one of the functions (abbreviated):
GetNumber(char *sNumber, int *length); <==returns int
I change it to VB.net:
GetNumber Lib "Foo.dll" (ByVal Number As IntPtr, ByRef Length As Int32) As Int32 '(intent is to return data into locations as pointed to by Number and Length pointers which are passed in; Int32 is return value = error code)
Here is VB.net code:
Sub Bar Dim Result As Int32 Dim DataLength As Int32 Dim NumberData As IntPtr = Marshal.AllocHGlobal(128) Dim TempData(128) As Byte Result = GetNumber(NumberData, DataLength) If Result = SuccessFlag Then Marshal.Copy(NumberData, TempData, 0, DataLength) lblNumber.Text = System.Text.ASCIIEncoding.ASCII.GetString(TempData) Else MessageBox.Show("Error reading Number.", "Data error", MessageBoxButtons.OK, MessageBoxIcon.Warning) lblNumber.Text = "ERROR" End If Marshal.FreeHGlobal(NumberData) End Sub
This works, but I barely understand why.
-------------------------------------------------
Here is second function, similar to first (abbreviated):
GetOtherNumber (bool c_flag, char *Number1, BYTE *Number2, int * length); '<==returns int
I change it to VB.net:
GetOtherNumber Lib "Foo.dll" (ByVal DataFlag As Boolean, ByVal DataString As String, ByVal DataData As IntPtr, ByRef Length As Int32) As Int32 '(intent is to return data into locations as pointed to by DataData and Length pointers which are passed in; DataFlag and DataString are passed in as supporting data; Int32 is return value = error code)
Here is VB.net code:
Sub Bar Dim Result As Int32 Dim DataLength As Int32 Dim DataData As IntPtr = Marshal.AllocHGlobal(512) Dim TempData(512) As Byte Result = GetOtherNumber(1, "1234567812345678", DataData, DataLength) '<==Note: Both the "1" for bool DataFlag, and "1234567812345678", are same values as used by legacy calling function, which of course works If Result = SuccessFlag Then Marshal.Copy(DataData, TempData, 0, DataLength) lblOtherNumber.Text = System.Text.ASCIIEncoding.ASCII.GetString(TempData) Else MessageBox.Show("Error reading Other Number.", "Data error", MessageBoxButtons.OK, MessageBoxIcon.Warning) lblOtherNumber.Text = "ERROR" End If Marshal.FreeHGlobal(PINData) End Sub
This does not work! The DLL returns an error indicating that the call has a bad parameter(s). I tried to follow the example of the
first (working) call. Is "Char" vs "Byte" different? How do I make call work with BYTE pointer? Or is it another problem?
Thank you.
Charlie
Last edited: