5 Replies - 6348 Views - Last Post: 16 April 2009 - 08:36 AM Rate Topic: -----

#1 wrxhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-December 08

Passing strings from VB.net to C++ DLL and back

Posted 04 March 2009 - 10:00 AM

I've been pulling my hair out on this issue, and I can't find any topics that seem to help on this one.

I have a 3rd party unmanaged C++ DLL function which takes a string (char *) as one of its parameters then modifies that string internally. Here's the code:

Bool get_signal_desc (char *dev_id, char *desc_string)
{
	strcpy(desc_string,"");
	return (get_dev_signal_name (dev_id, desc_string));
}



The call to get_dev_signal_name modifies the desc_string parameter to give you the name of a signal in our system.

I need to pass a string to that DLL function and then have that changed value available to my vb.net program. Here's my vb.net code:

<DllImport("D:\api.dll")> _
	Public Shared Function get_signal_desc(ByVal sig As String, ByVal desc As String) As Boolean
	End Function



Here's how I envoke the function in my vb.net code:

Dim signal As String
Dim desc As String
desc = Space(40) 'allocating space for string here
Dim result As Boolean
signal = "31025"
result = get_signal_desc(signal, desc)
MessageBox.Show(desc.ToString)
MessageBox.Show(result)



Currently this code runs without exception, but desc shows 40 spaces when displayed to the messagebox. I've tried StringBuilders, but they don't seem to work. I've also tried changing the 2nd parameter to be ByRef but this causes a read/write memory exception.

I need desc to return what it gets modified in the C++ DLL function. How can I do this? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing strings from VB.net to C++ DLL and back

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Passing strings from VB.net to C++ DLL and back

Posted 04 March 2009 - 10:46 AM

This may not be useful, as it's VB6 rather than .NET, but in a similar case passed a string buffer to a C DLL like this:
Private Declare Function GetData Lib "mydata.DLL" ( _
    ByVal szData As String) As Long
'...
Dim strBuf As String
Dim iNullCharPos as Integer

strBuf = String(256, vbNullChar)
' Function call
If (GetData(strBuf) = 0) Then
    iNullCharPos = InStr(strBuf, vbNullChar)
    If iNullCharPos > 1 Then
        strBuf = Left$(strBuf, iNullCharPos - 1)
    End If
End If


Was This Post Helpful? 0
  • +
  • -

#3 wrxhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-December 08

Re: Passing strings from VB.net to C++ DLL and back

Posted 04 March 2009 - 11:21 AM

View PostJackOfAllTrades, on 4 Mar, 2009 - 09:46 AM, said:

This may not be useful, as it's VB6 rather than .NET, but in a similar case passed a string buffer to a C DLL like this:
Private Declare Function GetData Lib "mydata.DLL" ( _
    ByVal szData As String) As Long
'...
Dim strBuf As String
Dim iNullCharPos as Integer

strBuf = String(256, vbNullChar)
' Function call
If (GetData(strBuf) = 0) Then
    iNullCharPos = InStr(strBuf, vbNullChar)
    If iNullCharPos > 1 Then
        strBuf = Left$(strBuf, iNullCharPos - 1)
    End If
End If



I'm not sure what this is doing exactly. It looks like its determining where the null terminator is using only that part of the string in the vb code.

That doesn't really help because my problem is that the string i'm passing doesn't get modified by the C DLL. If I initialize desc to 40 spaces or to a value like "junk", that value is what is returned by the DLL when it returns. I figure this has something to do with it passing the string by Value rather than by reference.

So what I need to do is pass desc by reference, which I have tried by changing the vb code function prototype for the second parameter to ByRef, but this causes a read/write memory exception. So how do i pass a string by reference without getting an exception?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Passing strings from VB.net to C++ DLL and back

Posted 04 March 2009 - 11:52 AM

Are you sure the function is returning successfully? It looks like if
get_dev_signal_name (dev_id, desc_string)
returns false (and subsequently get_signal_desc() does as well), you will get an empty string back.
Was This Post Helpful? 0
  • +
  • -

#5 wrxhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-December 08

Re: Passing strings from VB.net to C++ DLL and back

Posted 04 March 2009 - 12:03 PM

View PostJackOfAllTrades, on 4 Mar, 2009 - 10:52 AM, said:

Are you sure the function is returning successfully? It looks like if
get_dev_signal_name (dev_id, desc_string)
returns false (and subsequently get_signal_desc() does as well), you will get an empty string back.


Yeah, i'm getting True back from the function, meaning it worked.
Was This Post Helpful? 0
  • +
  • -

#6 bradhall31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-April 09

Re: Passing strings from VB.net to C++ DLL and back

Posted 16 April 2009 - 08:36 AM

try this:

Declare Ansi Function get_signal_desc Lib "mydata.DLL" (ByVal sig As String, ByVal desc As String) As Boolean


I hope it helps

Brad


View Postwrxhokie, on 4 Mar, 2009 - 10:21 AM, said:

View PostJackOfAllTrades, on 4 Mar, 2009 - 09:46 AM, said:

This may not be useful, as it's VB6 rather than .NET, but in a similar case passed a string buffer to a C DLL like this:
Private Declare Function GetData Lib "mydata.DLL" ( _
    ByVal szData As String) As Long
'...
Dim strBuf As String
Dim iNullCharPos as Integer

strBuf = String(256, vbNullChar)
' Function call
If (GetData(strBuf) = 0) Then
    iNullCharPos = InStr(strBuf, vbNullChar)
    If iNullCharPos > 1 Then
        strBuf = Left$(strBuf, iNullCharPos - 1)
    End If
End If



I'm not sure what this is doing exactly. It looks like its determining where the null terminator is using only that part of the string in the vb code.

That doesn't really help because my problem is that the string i'm passing doesn't get modified by the C DLL. If I initialize desc to 40 spaces or to a value like "junk", that value is what is returned by the DLL when it returns. I figure this has something to do with it passing the string by Value rather than by reference.

So what I need to do is pass desc by reference, which I have tried by changing the vb code function prototype for the second parameter to ByRef, but this causes a read/write memory exception. So how do i pass a string by reference without getting an exception?

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1