dcsimg
Results 1 to 8 of 8

Thread: problem passing a ref to a string to a dll both platforms x64

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Location
    vermont usa
    Posts
    72

    Unhappy problem passing a ref to a string to a dll both platforms x64

    my dll is and needs to be x64. I have in the past passed reference strings to a dll x32 but when I try to do the same thing with both platforms x64 it doesn't work.

    more explicitly my vb declare is:

    Declare Sub stringbyref_ Lib "c:\davids\directory_all_dlls\hiprec1.dll" (s As String)


    and my c++ dll function is:

    void FAR PASCAL stringbyref_(LPSTR*p_s)

    {



    }


    when the code is run p_s is a bad pointer!

    if you can help I would appreciate it.


    david

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,372

    Re: problem passing a ref to a string to a dll both platforms x64

    Your unmanaged code is using a pointer to a pointer so you would need to pass a reference to a reference type to make it equivalent, i.e. declare your 's' parameter ByRef. I think that you could also declare your parameter as type StringBuilder and pass it ByVal.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Location
    vermont usa
    Posts
    72

    Re: problem passing a ref to a string to a dll both platforms x64

    I did that (by ref) and any other combinations I could think of. no luck. do you know why I am failing with x64 and succeeding with x32.

    let me also add that if I pass s by value and receive it with LPSTR s. everything is ok. the actual string is passed. its the passback from the dll to vb that doesn't work with x64 as a property in both projects.
    Last edited by follys; Apr 30th, 2014 at 06:48 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,372

    Re: problem passing a ref to a string to a dll both platforms x64

    Are you using the same unmanaged DLL in each case or do you have 32-bit and 64-bit versions of that?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Location
    vermont usa
    Posts
    72

    Re: problem passing a ref to a string to a dll both platforms x64

    I have two projects. one an vb.exe which calls the dll - another project.

    the dll (c++) has configuration "active(debug)" platform "active(x64)

    I am passing from a vb application (.exe) that has configuration "active(debug)" platform "active(anycpu)" -- no other choices are available -- and target cpu "x64" . this is necessary.

    david

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Location
    vermont usa
    Posts
    72

    Re: problem passing a ref to a string to a dll both platforms x64

    my error! I apologize. it works as advertised.

    the declaration that works in the dll is:

    void FAR PASCAL stringbyref_(LPSTR p_s)

    {

    }

    and the calling declaration from vb is:

    Declare Sub stringbyref_ Lib "c:\davids\directory_all_dlls\hiprec1.dll" (s As String)

    note that:

    Declare Sub stringbyref_ Lib "c:\davids\directory_all_dlls\hiprec1.dll" (byref s As String)

    crashes when returning from the dll.

    sorry for inconvenience,


    david

  7. #7

    Re: problem passing a ref to a string to a dll both platforms x64

    What's the error when it crashes?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Location
    vermont usa
    Posts
    72

    Re: problem passing a ref to a string to a dll both platforms x64

    Running the code from vb we get the error:
    An unhandled exception of type 'System.AccessViolationException' occurred in ffff.exe

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Running the code from the dll we get:
    A buffer overrun has occurred in ffff.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.
    Do note that when the code reaches the dll p_s has junk in it.

    However if you do wish to declare s in vb as byref s as string, then the declaration in the dll
    Using LPSTR* works. But if you declare s in vb as s as string alone then using LPSTR* in the dll crashes.

    it seems clear that LPSTR and LPSTR* are different objects in c++. what we see here is that byref s as string and simply s as string are not equivalent. declaring s as string gives a pointer to the string in the dll and byref s as string gives a pointer to a pointer to the string.
    as I am just starting the x64 migration the above had me very concerned.
    but all is well now and it works as you should expect it to.

    best wishes
    david

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width