Pointer in API making life hell

A

AAVarda

Hello there.
There is a DLL file which contains a function as below. It will return me
the device name i.e pcDevName

ULONG MTMICRGetDevice (
DWORD dwDeviceContext,
char *pcDevName
);

Example given to use the above function is as below
-----------------------------------------
#define DEVICE_NAME_LEN 128
int i=1;
DWORD dwResult;
char pcDevName[DEVICE_NAME_LEN] = "";
while ((dwResult =
MTMICRGetDevice(i,(char*)pcDevName))!=MICR_ST_DEVICE_NOT_FOUND
{
//Device found, increment the device number
i++;
}

My defination in VB .NET to get the pcDevName
--------------------------------------
Declare Auto Function fGetDeviceName Lib "mtmcrapi.dll" Alias
"MTMICRGetDevice" (ByVal intNum As Integer, ByRef strDName As Char) As
Integer

Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdConnect.Click

Dim strMy As Char

Dim intI As Integer

Dim strRst As Integer

intI = 2

strRst = Win32.fGetDeviceName(intI, strMy)

MessageBox.Show(strMy.ToString, strRst.ToString)

End Sub

The function is returning the value 0 (that means ok) but the device name is
one char junk.
-WHAT IS WRONG???
-Is there a way to get the data by using the memory address in VB .NET
 
B

Bart Mermuys

Hi,

AAVarda said:
Hello there.
There is a DLL file which contains a function as below. It will return me
the device name i.e pcDevName

ULONG MTMICRGetDevice (
DWORD dwDeviceContext,
char *pcDevName
);

Example given to use the above function is as below
-----------------------------------------
#define DEVICE_NAME_LEN 128
int i=1;
DWORD dwResult;
char pcDevName[DEVICE_NAME_LEN] = "";
while ((dwResult =
MTMICRGetDevice(i,(char*)pcDevName))!=MICR_ST_DEVICE_NOT_FOUND
{
//Device found, increment the device number
i++;
}

My defination in VB .NET to get the pcDevName
--------------------------------------
Declare Auto Function fGetDeviceName Lib "mtmcrapi.dll" Alias
"MTMICRGetDevice" (ByVal intNum As Integer, ByRef strDName As Char) As
Integer

Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdConnect.Click

Dim strMy As Char

Dim intI As Integer

Dim strRst As Integer

intI = 2

strRst = Win32.fGetDeviceName(intI, strMy)

MessageBox.Show(strMy.ToString, strRst.ToString)

End Sub

The function is returning the value 0 (that means ok) but the device name
is one char junk.
-WHAT IS WRONG???
-Is there a way to get the data by using the memory address in VB .NET

Try:
Imports System.Text

Declare Ansi Function fGetDeviceName Lib "mtmcrapi.dll" Alias
"MTMICRGetDevice" (ByVal Context As Integer, _
ByVal NameSB As StringBuilder) As Integer

Dim ret As Integer
Dim context As Integer
Dim nameSB As New StringBuilder(128)

context = 2
ret = Win32.fGetDeviceName( context, nameSB )

MessageBox.Show( nameSB.ToString(), ret.ToString() )

hth,
Greetings
 
A

AAVarda

THANK YOU :) you guys are really really gr8. Problem solved
Bart Mermuys said:
Hi,

AAVarda said:
Hello there.
There is a DLL file which contains a function as below. It will return me
the device name i.e pcDevName

ULONG MTMICRGetDevice (
DWORD dwDeviceContext,
char *pcDevName
);

Example given to use the above function is as below
-----------------------------------------
#define DEVICE_NAME_LEN 128
int i=1;
DWORD dwResult;
char pcDevName[DEVICE_NAME_LEN] = "";
while ((dwResult =
MTMICRGetDevice(i,(char*)pcDevName))!=MICR_ST_DEVICE_NOT_FOUND
{
//Device found, increment the device number
i++;
}

My defination in VB .NET to get the pcDevName
--------------------------------------
Declare Auto Function fGetDeviceName Lib "mtmcrapi.dll" Alias
"MTMICRGetDevice" (ByVal intNum As Integer, ByRef strDName As Char) As
Integer

Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdConnect.Click

Dim strMy As Char

Dim intI As Integer

Dim strRst As Integer

intI = 2

strRst = Win32.fGetDeviceName(intI, strMy)

MessageBox.Show(strMy.ToString, strRst.ToString)

End Sub

The function is returning the value 0 (that means ok) but the device name
is one char junk.
-WHAT IS WRONG???
-Is there a way to get the data by using the memory address in VB .NET

Try:
Imports System.Text

Declare Ansi Function fGetDeviceName Lib "mtmcrapi.dll" Alias
"MTMICRGetDevice" (ByVal Context As Integer, _
ByVal NameSB As StringBuilder) As Integer

Dim ret As Integer
Dim context As Integer
Dim nameSB As New StringBuilder(128)

context = 2
ret = Win32.fGetDeviceName( context, nameSB )

MessageBox.Show( nameSB.ToString(), ret.ToString() )

hth,
Greetings
 
D

Dragon

Hello,
-WHAT IS WRONG???

Whong is that a char* in C is not exactly a *pointer to char*. It's a
*pointer to a null-terminated string*.
Thus, it should be declared as String in VB (or StringBuilder if it's
going to be changed) if you don't want to get a "one char junk".

You should try this:

~
Declare Function fGetDeviceName Lib "mtmcrapi.dll" Alias
"MTMICRGetDevice" ( _
ByVal dwDeviceContext As Integer, _
<MarshalAs(UnmanagedType.LPStr)> ByVal pcDevName As StringBuilder _
) As Integer
~
-Is there a way to get the data by using the memory address in VB .NET

Look at Shared Marshal.PtrTo* methods.

Hope this helps,
Roman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top