DELPHI代码如下:
library print_dll;
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
strutils;function Prt_call(code_string: pchar; code_name: pchar): Boolean;stdcall;
var
prtstr: string;
filehandle: integer;
return_str:Boolean;
begin
return_str:=False;
prtstr := Prt_code(code_string,code_name);//此处为其他DLL定义的函数
filehandle := fileopen('LPT1', fmOpenWrite);
if filehandle > 0 then
begin
filewrite(filehandle, prtstr[1], length(prtstr));
return_str:=True;
end;
fileclose(filehandle);
result:= return_str;
end;{$R *.RES}
Exports Prt_call;begin
end.
C#调用代码如下:
[DllImport(@"D:\我的工作\WindowsApplication1 bin\print_dll.dll",CharSet=CharSet.Auto,EntryPoint="Prt_call")]
public static extern bool Prt_call(string code,string name);
private void button_print_Click(object sender, System.EventArgs e)
{
bool aa=Prt_call("12AV56789012345678901234567890","");
}
传值时,传30位字符串,但打印出来只有11位,在DELPHI里用SHOWMESSAGE显示,显示获得的字符串值为"1",请教高手们这该如何解决,在线等待,急!!!!
library print_dll;
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
strutils;function Prt_call(code_string: pchar; code_name: pchar): Boolean;stdcall;
var
prtstr: string;
filehandle: integer;
return_str:Boolean;
begin
return_str:=False;
prtstr := Prt_code(code_string,code_name);//此处为其他DLL定义的函数
filehandle := fileopen('LPT1', fmOpenWrite);
if filehandle > 0 then
begin
filewrite(filehandle, prtstr[1], length(prtstr));
return_str:=True;
end;
fileclose(filehandle);
result:= return_str;
end;{$R *.RES}
Exports Prt_call;begin
end.
C#调用代码如下:
[DllImport(@"D:\我的工作\WindowsApplication1 bin\print_dll.dll",CharSet=CharSet.Auto,EntryPoint="Prt_call")]
public static extern bool Prt_call(string code,string name);
private void button_print_Click(object sender, System.EventArgs e)
{
bool aa=Prt_call("12AV56789012345678901234567890","");
}
传值时,传30位字符串,但打印出来只有11位,在DELPHI里用SHOWMESSAGE显示,显示获得的字符串值为"1",请教高手们这该如何解决,在线等待,急!!!!
所以你的dll必须这样定义:
function Prt_call(code_string: pWideChar; code_name: pWideChar): Boolean;stdcall;
然后简单地能通过string的转换即可:
x:string;
code_string_c:pchar;
...
x:=code_string;
code_string_c:=pchar(x);
prtstr := Prt_code(code_string_c,code_name);//此处为其他DLL定义的函数
...