Pointer in API making life hell

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AAVarda

    #1

    Pointer in API making life hell

    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*)pcDev Name))!=MICR_ST _DEVICE_NOT_FOU ND
    {
    //Device found, increment the device number
    i++;
    }

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

    Private Sub cmdConnect_Clic k(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles cmdConnect.Clic k

    Dim strMy As Char

    Dim intI As Integer

    Dim strRst As Integer

    intI = 2

    strRst = Win32.fGetDevic eName(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



  • Bart Mermuys

    #2
    Re: Pointer in API making life hell

    Hi,

    "AAVarda" <asgar@data-aware.com> wrote in message
    news:OIBJlLY0FH A.2792@tk2msftn gp13.phx.gbl...[color=blue]
    > 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*)pcDev Name))!=MICR_ST _DEVICE_NOT_FOU ND
    > {
    > //Device found, increment the device number
    > i++;
    > }
    >
    > My defination in VB .NET to get the pcDevName
    > --------------------------------------
    > Declare Auto Function fGetDeviceName Lib "mtmcrapi.d ll" Alias
    > "MTMICRGetDevic e" (ByVal intNum As Integer, ByRef strDName As Char) As
    > Integer
    >
    > Private Sub cmdConnect_Clic k(ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles cmdConnect.Clic k
    >
    > Dim strMy As Char
    >
    > Dim intI As Integer
    >
    > Dim strRst As Integer
    >
    > intI = 2
    >
    > strRst = Win32.fGetDevic eName(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
    >[/color]

    Try:
    Imports System.Text

    Declare Ansi Function fGetDeviceName Lib "mtmcrapi.d ll" Alias
    "MTMICRGetDevic e" (ByVal Context As Integer, _
    ByVal NameSB As StringBuilder) As Integer

    Dim ret As Integer
    Dim context As Integer
    Dim nameSB As New StringBuilder(1 28)

    context = 2
    ret = Win32.fGetDevic eName( context, nameSB )

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

    hth,
    Greetings


    Comment

    • AAVarda

      #3
      Re: Pointer in API making life hell

      THANK YOU :) you guys are really really gr8. Problem solved
      "Bart Mermuys" <bmermuys.nospa m@hotmail.com> wrote in message
      news:ucSKtbY0FH A.1252@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi,
      >
      > "AAVarda" <asgar@data-aware.com> wrote in message
      > news:OIBJlLY0FH A.2792@tk2msftn gp13.phx.gbl...[color=green]
      >> 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*)pcDev Name))!=MICR_ST _DEVICE_NOT_FOU ND
      >> {
      >> //Device found, increment the device number
      >> i++;
      >> }
      >>
      >> My defination in VB .NET to get the pcDevName
      >> --------------------------------------
      >> Declare Auto Function fGetDeviceName Lib "mtmcrapi.d ll" Alias
      >> "MTMICRGetDevic e" (ByVal intNum As Integer, ByRef strDName As Char) As
      >> Integer
      >>
      >> Private Sub cmdConnect_Clic k(ByVal sender As System.Object, ByVal e As
      >> System.EventArg s) Handles cmdConnect.Clic k
      >>
      >> Dim strMy As Char
      >>
      >> Dim intI As Integer
      >>
      >> Dim strRst As Integer
      >>
      >> intI = 2
      >>
      >> strRst = Win32.fGetDevic eName(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
      >>[/color]
      >
      > Try:
      > Imports System.Text
      >
      > Declare Ansi Function fGetDeviceName Lib "mtmcrapi.d ll" Alias
      > "MTMICRGetDevic e" (ByVal Context As Integer, _
      > ByVal NameSB As StringBuilder) As Integer
      >
      > Dim ret As Integer
      > Dim context As Integer
      > Dim nameSB As New StringBuilder(1 28)
      >
      > context = 2
      > ret = Win32.fGetDevic eName( context, nameSB )
      >
      > MessageBox.Show ( nameSB.ToString (), ret.ToString() )
      >
      > hth,
      > Greetings
      >[/color]


      Comment

      • Dragon

        #4
        Re: Pointer in API making life hell

        Hello,
        [color=blue]
        > -WHAT IS WRONG???[/color]

        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.d ll" Alias
        "MTMICRGetDevic e" ( _
        ByVal dwDeviceContext As Integer, _
        <MarshalAs(Unma nagedType.LPStr )> ByVal pcDevName As StringBuilder _
        ) As Integer
        ~
        [color=blue]
        > -Is there a way to get the data by using the memory address in VB .NET[/color]

        Look at Shared Marshal.PtrTo* methods.
        [color=blue]
        >
        >
        >[/color]

        Hope this helps,
        Roman


        Comment

        Working...