Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Sending and catching strings from a DLL

Sending and catching strings from a DLL

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Swim 13nrv
    wrote on last edited by
    #1

    Hello, I have difficulties in using a DLL I was given. So I can't modify the code of this DLL, but I know that it works, since I tested it succesfully with a C program (with the .h and .lib files that were given with it). My problem is I have to call it in VB .NET. Here is the function that I have to call in the DLL: void myfunction( long * param_one, long param_two, unsigned char * param_three, unsigned char * param_four) Parameters 1 and 4 are outputs and parameters 2 and 3 are inputs. And here is how I declare and call it in my VB Code: Private Declare Sub myfunction Lib "mydll" _ (ByRef param_one As Long, _ ByVal param_two As Long, _ ByVal param_three As String, _ ByVal param_four As String) Call myfunction(a,b,c,d) It seems the string input (param_three) doesn't reach the DLL, which I know from the return code (param_one). I've tested with ByRefs for the strings but it doesn't work either. Maybe is it a problem with the fact that these are unsigned char * ? I am a brand new DLL user so I may have made mistakes... If someone has an idea, I would really appreciate it.

    D T 2 Replies Last reply
    0
    • S Swim 13nrv

      Hello, I have difficulties in using a DLL I was given. So I can't modify the code of this DLL, but I know that it works, since I tested it succesfully with a C program (with the .h and .lib files that were given with it). My problem is I have to call it in VB .NET. Here is the function that I have to call in the DLL: void myfunction( long * param_one, long param_two, unsigned char * param_three, unsigned char * param_four) Parameters 1 and 4 are outputs and parameters 2 and 3 are inputs. And here is how I declare and call it in my VB Code: Private Declare Sub myfunction Lib "mydll" _ (ByRef param_one As Long, _ ByVal param_two As Long, _ ByVal param_three As String, _ ByVal param_four As String) Call myfunction(a,b,c,d) It seems the string input (param_three) doesn't reach the DLL, which I know from the return code (param_one). I've tested with ByRefs for the strings but it doesn't work either. Maybe is it a problem with the fact that these are unsigned char * ? I am a brand new DLL user so I may have made mistakes... If someone has an idea, I would really appreciate it.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It depends on what the .DLL is doing with the string. If it's creating a string for return, then what you did should work. If it's expecting a string BUFFER passed in (which is what this looks like), you have to pass in a buffer for a string and not just a pointer to en empty string. The buffered version can be done with a StringBuilder instead of a String.

      Private Declare Sub myFunction Lib "mydll" ( _
      ByRef param_one As Long, _
      ByVal param_two As Long, _
      ByVal param_three As StringBuilder, _
      ...)
       
      ' To call the thing, create the buffer first.
      Dim buffer As new StringBuilder( 512 )
      myFunction( a, b, buffer, d )

      You can get more information and techniques from here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      S 1 Reply Last reply
      0
      • S Swim 13nrv

        Hello, I have difficulties in using a DLL I was given. So I can't modify the code of this DLL, but I know that it works, since I tested it succesfully with a C program (with the .h and .lib files that were given with it). My problem is I have to call it in VB .NET. Here is the function that I have to call in the DLL: void myfunction( long * param_one, long param_two, unsigned char * param_three, unsigned char * param_four) Parameters 1 and 4 are outputs and parameters 2 and 3 are inputs. And here is how I declare and call it in my VB Code: Private Declare Sub myfunction Lib "mydll" _ (ByRef param_one As Long, _ ByVal param_two As Long, _ ByVal param_three As String, _ ByVal param_four As String) Call myfunction(a,b,c,d) It seems the string input (param_three) doesn't reach the DLL, which I know from the return code (param_one). I've tested with ByRefs for the strings but it doesn't work either. Maybe is it a problem with the fact that these are unsigned char * ? I am a brand new DLL user so I may have made mistakes... If someone has an idea, I would really appreciate it.

        T Offline
        T Offline
        thealca
        wrote on last edited by
        #3

        For all I know the type of parameters three and four (string) should be LPSTR. void myfunction(long * param_one, long param_two,LPSTR * param_three,LPSTR * param_four) . . . we have same problem encountered when i create a dll in VC++, and it works when I used LPSTR, u can try this. I hope it helps

        S 1 Reply Last reply
        0
        • D Dave Kreskowiak

          It depends on what the .DLL is doing with the string. If it's creating a string for return, then what you did should work. If it's expecting a string BUFFER passed in (which is what this looks like), you have to pass in a buffer for a string and not just a pointer to en empty string. The buffered version can be done with a StringBuilder instead of a String.

          Private Declare Sub myFunction Lib "mydll" ( _
          ByRef param_one As Long, _
          ByVal param_two As Long, _
          ByVal param_three As StringBuilder, _
          ...)
           
          ' To call the thing, create the buffer first.
          Dim buffer As new StringBuilder( 512 )
          myFunction( a, b, buffer, d )

          You can get more information and techniques from here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          S Offline
          S Offline
          Swim 13nrv
          wrote on last edited by
          #4

          Thanks for your help. Although I'm now using a StringBuilder, the parameter doesn't seem to get in the DLL... I really wonder why this doesn't work :(

          1 Reply Last reply
          0
          • T thealca

            For all I know the type of parameters three and four (string) should be LPSTR. void myfunction(long * param_one, long param_two,LPSTR * param_three,LPSTR * param_four) . . . we have same problem encountered when i create a dll in VC++, and it works when I used LPSTR, u can try this. I hope it helps

            S Offline
            S Offline
            Swim 13nrv
            wrote on last edited by
            #5

            Yes, I've already read that somewhere, but the problem is I can't modify the DLL... Thanks anyway.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups