用Delphi写的DLL用C#怎么调用?
注册时说找不到指定的模板 ,是不是DELPHI的DLL有问题,应该怎么写?
[解决办法]
library helloworld;
uses
SysUtils,
Classes;
{$R *.res}
function GetString:string;stdcall;
begin
Result:= 'hello world ';
end;
exports
GetString ;
begin
end.
///
[DllImport( "kernel32 ",EntryPoint= "LoadLibrary ",SetLastError=true)]
static extern IntPtr LoadLibrary(string lpLibName);
[DllImport( "kernel32 ",EntryPoint= "GetProcAddress ",SetLastError=true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport( "kernel32 ",EntryPoint= "FreeLibrary ",SetLastError=true)]
static extern bool FreeLibrary(IntPtr hModule);
public delegate string GetString();
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hmod=LoadLibrary(@ "D:\helloworld.dll ");
IntPtr funcaddr=GetProcAddress(hmod, "GetString ");
GetString myfunc = (GetString) Marshal.GetDelegateForFunctionPointer( funcaddr, typeof( GetString ) );
MessageBox.Show( myfunc() );
FreeLibrary(hmod);
}
[解决办法]
够详细了,不过尽量用PChar,不要用delphi特有的变量。