P/Invoke call to external DLL  
Author Message
Patrick Schmid





PostPosted: Visual C# Language, P/Invoke call to external DLL Top

I am stuck trying to get a P/Invoke correctly done to a 3rd party DLL.
The DLL is called DNA.DLL. I have other P/Invoke calls working to it, but this one just doesn't want to. The problem is that it returns a string via a buffer. Whatever I try to declare as buffer (ref string, out string, ref StringBuilder, out StringBuilder) ends up causing a System.AccessViolationException in .NET 2 (C#, VS 2005).
Here is the info I have about this call:
Delphi: function DNA_Param(param,value:pChar; value_size:integer):integer; stdcall;
VBasic6: Public Function DNA_Param(ByVal param As String, ByRef value As String, ByVal value_size As Integer) As Integer
C++: int __stdcall DNA_Param(char *param,char *value,int value_size);
Request to the DNA Client to return the <value> of the <param> parameter stored in the CDM on the user’s computer. <value> should be a buffer of 256 bytes or greater. <value_size> should indicate the size of buffer allocated. If DNA_Param returns “0”, then <value> will contain the value of the <param> from the CDM. If DNA_Param returns “1”, then a problem occurred with the DNA.DLL.
----
A VC++ (unmanaged) sample implementation:
char buffer[255];
DNA_Param("ACTIVATION_CODE",buffer,sizeof(buffer));
using the signature for DNA_Param shown above
----
A VB6 sample implementation:
Public Function DNA_Param(ByVal param As String, ByRef value As String, ByVal value_size As Integer) As Integer
Dim buffer As String

buffer = Space(value_size)
DNA_Param = DNA_Param_DLL(param, buffer, value_size)
value = NullString(buffer)
End Function
Private Function NullString(ByVal s As String)
Dim i As Integer
i = InStr(s, vbNullChar)
If i = 0 Then
NullString = ""
Else
NullString = Left(s, i - 1)
End If
End Function
Private Function Space(Size As Integer) As String
Dim returnString as String
Dim i As Integer
Dim upperBound As Integer

upperBound = Size - 1

For i = 0 to upperBound

returnString = returnString + " "

Next

return returnString
End Function
----
Can anyone please help me with this
Thank you very much in advance,

Patrick Schmid


Visual C#11  
 
 
Mike Danes





PostPosted: Visual C# Language, P/Invoke call to external DLL Top

Have you tried something like this:

[DllImport("dpa.dll", Charset=CharSet.Ansi)]

public static extern int DNA_Param(string param, StringBuilder value, int valueSize) ;

void test()

{

StringBuilder sb = new StringBuilder(256);

DNA_Param("ACTIVATION_CODE", sb, sb.Capacity);
}

You don't need ref/out for that string because it's not a pointer to a char pointer.


 
 
Patrick Schmid





PostPosted: Visual C# Language, P/Invoke call to external DLL Top

Using
[DllImport("Dna.dll")]
int DNA_Param(string param, StringBuilder value,int value_size);

and then StringBuilder buffer=new StringBuilder(255);
DNA_Param("xxx", buffer, 255); did it

Patrick

 
 
Patrick Schmid





PostPosted: Visual C# Language, P/Invoke call to external DLL Top

Thanks! Didn't see your reply until I posted what I had figure out...(I forgot that these forums don't send you emails on replies..)