I don't need to pass values back to the caller - just need them to get to
the DLL correctly. Also, I cannot use the pChar type (or any pointer) as
my code cannot be UNSAFE code. My DLL will be imported into a MS SQL
server (using CLR integration) and so must be 100% .NET and cannot be
unsafe. Is this some sort of Unicode thing where due to how the framework
handles strings it has converted the passed string to where the second byte
is a #0 ?
Any help would be appreciated. Thanks,
Eric Gooden
Code from DLL
procedure TestMe( var sVarString: String; sNormalString: String; out
sOutString: String;
iNormalInteger: Integer; var iVarInteger: Integer; out iOutInteger:
Integer;
sbStringBuilder: StringBuilder);
var sw: StreamWriter;
begin
// Writing results to local text file for debugging
sw:= StreamWriter.Create( 'c:\eric.txt' );
sw.WriteLine( 'sVarString: ' + sVarString );
sw.WriteLine( 'sNormalString: ' + sNormalString );
sw.WriteLine( 'sOutString: ' + sOutString );
sw.WriteLine( 'iNormalInteger: ' + iNormalInteger.ToString );
sw.WriteLine( 'iVarInteger: ' + iVarInteger.ToString );
sw.WriteLine( 'iOutInteger: ' + iOutInteger.ToString );
sw.WriteLine( 'StringBuilder: ' + sbStringBuilder.ToString );
sw.Close;
end;
Code from calling program
// Interface Section
procedure TestMe( var sVarString: String; sNormalString: String; out
sOutString: String;
iNormalInteger: Integer; var iVarInteger: Integer; out iOutInteger:
Integer;
sbStringBuilder: StringBuilder); external 'Library1.dll';
// Implementation Section
.........
procedure Mainf.Button3_Click(sender: System.Object; e: System.EventArgs);
var sTemp1, sTemp2, sTemp3: String;
iTemp1, iTemp2, iTemp3: Integer;
MyArray: array of Char;
sbStringBuilder: StringBuilder;
begin
// Prepare the parms
sTemp1:= '1egooden1';
sTemp2:= '2egooden2';
sTemp3:= '3egooden3';
iTemp1:= 1931;
iTemp2:= 2932;
iTemp3:= 3933;
sbStringBuilder:= StringBuilder.Create;
sbStringBuilder.Capacity:= 128;
sbStringBuilder.Append( 'Eric Gooden was here !' );
// Call the external procedure
TestMe( sTemp1, sTemp2, sTemp3, iTemp1, iTemp2, iTemp3,
sbStringBuilder );
end;
.........
Results are looking like this:
sVarString: 1
sNormalString: 2
sOutString:
iNormalInteger: 1931
iVarInteger: 2932
iOutInteger: 3933
StringBuilder: E
> Is this some sort of Unicode thing where due to how the framework
> handles strings it has converted the passed string to where the second byte
> is a #0 ?
It sounds like a good guess. Have you tried to use a DllImport
attribute and specifying the CharSet as Ansi?
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
What I ended up using is replacing
var sVarString: String
with
[MarshalAs(UnmanagedType.LPWStr)] var sVarString: wideString;
So that the procedure declaration in the DLL looks like this
procedure TestMe([MarshalAs(UnmanagedType.LPWStr)] var sVarString:
wideString; [MarshalAs(UnmanagedType.LPWStr)] var sNormalString: wideString;
[MarshalAs(UnmanagedType.LPWStr)] out sOutString: wideString;
iNormalInteger: Integer; var iVarInteger: Integer; out iOutInteger:
Integer;
sbStringBuilder: StringBuilder); export;
This is allowing me to retrieve the full string. For some reason, it
doesn't work without the VAR in front of the string declaration (I don't
really want to pass the string back to the caller) - but I'll take what I
can get.
I am still working on the StringBuilder and getting that to work.
Eric Gooden
"Marc Rohloff [TeamB]" <"on request"> wrote in message
news:riyj74my4x67$.dlg@marcrohloff.com...