我并不关心C#中的代码,那不是我来写的(呵呵)。我只关系Delphi生成的dll。搞C#的人说不是标准的COM。要怎么写才是标准的啊。我的代码: library PInterface;uses Sharemem, SysUtils, Classes, ComServ, UInterface_Obj in 'UInterface_Obj.pas', UInterfaceBase in 'UInterfaceBase.pas', UInterface in 'UInterface.pas';{$R *.res} exports DllGetClassObject, DllCanUnloadNow, DllRegisterServer, DllUnregisterServer, SetShuiBiaoCJ, SetShuiBiaoLX, SetShuiBiaoFL, SetShuiBiaoKJXX;begin end.
unit UInterface;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Controls, Dialogs, UInterface_Obj;var SetInterface:TSetInterface;procedure InitInterface; function SetShuiBiaoCJ(I_ChangJiaBH:int64;S_ChangJiaMC:PChar):Boolean;export; stdcall; function SetShuiBiaoLX(I_BiaoLeiXBH:int64;S_LeiXingMC:PChar):Boolean;export; stdcall; implementation procedure InitInterface; begin if not Assigned(SetInterface) then SetInterface := TSetInterface.Create(nil); end;function SetCeBenXX(S_CH,S_ChaoBiaoYBH,S_ST:Pchar):Boolean;export; stdcall; begin Result := False; InitInterface; //数据检查 if S_CH = '' then Exit; if S_ChaoBiaoYBH = '' then Exit; if S_ST = '' then Exit; Result := SetInterface.SetCeBenXX(PChar(S_CH),PChar(S_ChaoBiaoYBH),PChar(S_ST)); end;function SetShuiBiaoCJ(I_ChangJiaBH:Int64;S_ChangJiaMC:PChar):Boolean;export; stdcall; begin Result := False; InitInterface; //数据检查 if I_ChangJiaBH = 0 then Exit; Result := SetInterface.SetShuiBiaoCJ(I_ChangJiaBH,S_ChangJiaMC); end; ............ ............
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);
}
Delphi写好的dll文件到没有安装DELPHI环境的机器上注册失败。
library PInterface;uses
Sharemem,
SysUtils,
Classes,
ComServ,
UInterface_Obj in 'UInterface_Obj.pas',
UInterfaceBase in 'UInterfaceBase.pas',
UInterface in 'UInterface.pas';{$R *.res}
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer,
SetShuiBiaoCJ,
SetShuiBiaoLX,
SetShuiBiaoFL,
SetShuiBiaoKJXX;begin
end.
Windows, Messages, SysUtils, Variants, Classes, Controls, Dialogs, UInterface_Obj;var
SetInterface:TSetInterface;procedure InitInterface;
function SetShuiBiaoCJ(I_ChangJiaBH:int64;S_ChangJiaMC:PChar):Boolean;export; stdcall;
function SetShuiBiaoLX(I_BiaoLeiXBH:int64;S_LeiXingMC:PChar):Boolean;export; stdcall;
implementation
procedure InitInterface;
begin
if not Assigned(SetInterface) then
SetInterface := TSetInterface.Create(nil);
end;function SetCeBenXX(S_CH,S_ChaoBiaoYBH,S_ST:Pchar):Boolean;export; stdcall;
begin
Result := False;
InitInterface;
//数据检查
if S_CH = '' then Exit;
if S_ChaoBiaoYBH = '' then Exit;
if S_ST = '' then Exit; Result := SetInterface.SetCeBenXX(PChar(S_CH),PChar(S_ChaoBiaoYBH),PChar(S_ST));
end;function SetShuiBiaoCJ(I_ChangJiaBH:Int64;S_ChangJiaMC:PChar):Boolean;export; stdcall;
begin
Result := False;
InitInterface;
//数据检查
if I_ChangJiaBH = 0 then Exit; Result := SetInterface.SetShuiBiaoCJ(I_ChangJiaBH,S_ChangJiaMC);
end;
............
............
又不是vb和c#
还com呢