vb.net strings passing Win32 Dlls - HELP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marfi95@yahoo.com

    vb.net strings passing Win32 Dlls - HELP

    I'm not sure what other groups this should go to, but I'm trying to
    write a Win32 DLL that I can call from my vb.net app. I'm having a
    real tough time, as this is printing a different char set than what I
    expect. I'm not sure if I'm doing something wrong or its not setup
    right, but it looks like Chinese characters coming back. Here is all
    the code:

    Win32 exported function:

    int CDllTest::TestF unc3(TCHAR* lStr, int* lLen)
    {
    TCHAR* x=NULL;

    x = (char *)calloc(10000, sizeof(char));
    strcpy(x, "abcabcd");
    strcpy(lStr, x);

    *lLen = strlen(lStr);
    return 0;
    }

    VB.net method:

    Declare Auto Function TestFunc3 Lib "TestDLL" (ByVal msg As
    StringBuilder, ByRef msgLength As IntPtr) As Integer

    Dim s1 As StringBuilder
    Dim i2 As IntPtr

    s1 = New StringBuilder(1 000)
    i1 = TestFunc3(s1, i2)
    Trace.WriteLine (s1.ToString & " " & i2.ToInt32)

    When trace writes s1.tostring, it seems like chinese characters,
    however the length is correct, so its got something to do with the way
    its marshalled I guess.

    Any help would be great !!!!!

    Mark

  • Mattias Sjögren

    #2
    Re: vb.net strings passing Win32 Dlls - HELP

    > x = (char *)calloc(10000, sizeof(char));

    You never free the memory you allocate here so the function is
    leaking.

    [color=blue]
    > strcpy(x, "abcabcd");
    > strcpy(lStr, x);[/color]

    Why not just strcpy(lStr, "abcabcd") directly?

    You may also want to pass in the size of the lStr buffer to make sure
    you don't overrun it.

    [color=blue]
    >Declare Auto Function TestFunc3 Lib "TestDLL" (ByVal msg As
    >StringBuilde r, ByRef msgLength As IntPtr) As Integer[/color]

    Since you used strcpy above I assume you're using an ANSI build of the
    DLL where TCHAR becomes char. In that case you shouldn't use the Auto
    modifier on your Declare statement because that will cause the string
    to be treated as Unicode on recent versions of Windows.

    And the msgLength parameter should be of type Integer, not IntPtr.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • marfi95@yahoo.com

      #3
      Re: vb.net strings passing Win32 Dlls - HELP

      Thanks Mattias !!!!!!

      It was the Auto causing it.

      Comment

      • marfi95@yahoo.com

        #4
        Re: vb.net strings passing Win32 Dlls - HELP

        One other thing mattias,

        Is it possible to reinitialize a StringBuilder object, or is the
        correct way just to keep reallocating new ones ? I want to just create
        1 instance of a stringbuilder, then just use it over and over again ?

        Comment

        • Mattias Sjögren

          #5
          Re: vb.net strings passing Win32 Dlls - HELP

          >or is the correct way just to keep reallocating new ones ?

          There's nothing wrong with it (unless this happens to be a very
          performance critical part of your code called in a tight loop and ou
          need to reduce the numbe of objects created).


          Mattias

          --
          Mattias Sjögren [C# MVP] mattias @ mvps.org
          http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          Please reply only to the newsgroup.

          Comment

          Working...