用delphi编写dll给vc调用问题(100)

  • 主题发起人 主题发起人 scorpioing
  • 开始时间 开始时间
S

scorpioing

Unregistered / Unconfirmed
GUEST, unregistred user!
本人用delphi写了一个对数据库进行操作的dll,给vc来调用,不知怎么回事,用vc调用的时候无法获取程序入口地址。不知是不是delphi编译生成dll的环境设置问题?麻烦富翁们给点建议!急等……!!!
 
dll中是否有用到string类型?
 
没有,原来用了,后来改成shortstring 在uses里面加了sharmem
 
把你的调用代码贴出来看看~~
 
所有类型shortstring改用pchar
 
检查函数大小写与delphi中定义大小写是否一致。
 
dll完成的功能就是把接收到的数据存到sqlserver数据中,请富翁们给看看library MySave;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses ShareMem, Windows, System, SysUtils, Variants, Classes, Adodb, math;{$R *.res}//定义接收采集端数据结构type TReceiveData = record NodeID: integer; COleDateTime: TDateTime; AngleX1: WORD; AngleY1: WORD; AngleX2: WORD; AngleY2: WORD; AngleX3: WORD; AngleY3: WORD; AngleX4: WORD; AngleY4: WORD; PullForce1: WORD; PullForce2: WORD; PullForce3: WORD; WindSpeed: WORD; WindOrient: WORD; Voltage: WORD; Temperature1: WORD; Temperature2: WORD; Temperature3: WORD; Temperature4: WORD; SensorTemp: WORD; Reserved0: WORD; Reserved1: WORD; Reserved2: WORD; Reserved3: WORD; Reserved4: WORD; Reserved5: WORD; Reserved6: WORD; Reserved7: WORD; Reserved8: WORD; end; TagData = ^TReceiveData; TMyArry = array of shortstring; TSQLArray = array[0..27] of double; TSQLSaveData = ^TSQLArray;var ADOConn: Tadoconnection; ADOQry: TADOQuery; ADOQryTable: TADOQuery;//角度参数换算function GetValueJiaoD(JD: integer): Double;var PI: Double;begin PI := 3.14; Result := JD; Result := (Result - 1024) / 1638; Result := ArcSin(Result) * 180 / PI;end;//拉力参数换算function GetValueLaL(LL: integer): Double;begin Result := LL;end;//风向参数换算function GetValueFengX(FX: integer): Double;begin Result := FX;end;//风速参数换算function GetValueFengS(FS: integer): Double;begin Result := FS; Result := Result / 7.5;end;//温度参数换算function GetValueWenD(WD: integer): Double;begin Result := WD; Result := (Result - 197) / (-1.083);end;//链接数据库 服务器名称:ServerID 用户名:UserName 用户密码:UserPWDfunction ConnectServer(ServerID, UserName, UserPWD: PChar): Boolean; stdcall;begin ADOConn := Tadoconnection.Create(nil); try ADOConn.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=True;Data Source=' + ServerID; ADOConn.Open(UserName, UserPWD); Result := True; except Result := False; end;end;//获取需要存储的表名称function GetTableName: TMyArry;var TableName: TMYArry; i: integer;begin ADOQryTable := TADOQuery.Create(nil); ADOQryTable.Connection := ADOConn; try with ADOQryTable do begin Close; SQL.Text := 'select TableName from TableINFO order by OrderID ASC'; Open; end; Setlength(TableName, ADOQryTable.RecordCount); ADOQryTable.First; for i := 0 to (ADOQryTable.RecordCount - 1) do begin TableName := ADOQryTable.FieldByName('TableName').AsString; ADOQryTable.Next; end; Result := TableName; except Exit; end;end;//保存数据到数据库:结构体指针procedure SaveData(a: TagData); stdcall;var SystemTime: TSystemTime; SaveDateTime: shortstring; ReceiveData: array of double; ReceiveTable: TMyArry; k: integer;begin GetLocalTime(SystemTime); SaveDateTime := IntToStr(SystemTime.wYear) + '-' + IntToStr(SystemTime.wMonth) + '-' + IntToStr(SystemTime.wDay) + ' ' + IntToStr(SystemTime.wHour) + ':' + IntToStr(SystemTime.wMinute) + ':' + IntToStr(SystemTime.wSecond); //获得需要存储数据 ReceiveTable := GetTableName; Setlength(ReceiveData, ADOQryTable.RecordCount); ReceiveData[0] := GetValueJiaoD(a.AngleX1); ReceiveData[1] := GetValueJiaoD(a.AngleY1); ReceiveData[2] := GetValueJiaoD(a.AngleX2); ReceiveData[3] := GetValueJiaoD(a.AngleY2); ReceiveData[4] := GetValueJiaoD(a.AngleX3); ReceiveData[5] := GetValueJiaoD(a.AngleY3); ReceiveData[6] := GetValueJiaoD(a.AngleX4); ReceiveData[7] := GetValueJiaoD(a.AngleY4); ReceiveData[8] := GetValueLaL(a.PullForce1); ReceiveData[9] := GetValueLaL(a.PullForce2); ReceiveData[10] := GetValueLaL(a.PullForce3); ReceiveData[11] := GetValueFengS(a.WindSpeed); ReceiveData[12] := GetValueFengX(a.WindOrient); ReceiveData[13] := a.Voltage; ReceiveData[14] := GetValueWenD(a.Temperature1); ReceiveData[15] := GetValueWenD(a.Temperature2); ReceiveData[16] := GetValueWenD(a.Temperature3); ReceiveData[17] := GetValueWenD(a.Temperature4); ReceiveData[18] := a.SensorTemp; //开始存储数据 ADOQry := TADOQuery.Create(nil); ADOQry.Connection := ADOConn; for k := 0 to (ADOQryTable.RecordCount - 1) do try with ADOQry do begin Close; SQL.Text := 'insert into ' + Trim(ReceiveTable[k]) + ' values(' + IntToStr(a.NodeID) + ',' + FloatToStr(ReceiveData[k]) + ',' + QuotedStr(SaveDateTime) + ')'; ExecSQL; end; except Exit; end;end;//保存数据到数据库:指针数组procedure SQLSaveData(NodeID: integer; l_temp: double; Pval: TSQLSaveData; count: integer); stdcall;var SystemTime: TSystemTime; SaveDateTime: shortstring; ReceiveTable: TMyArry; m: integer;begin GetLocalTime(SystemTime); SaveDateTime := IntToStr(SystemTime.wYear) + '-' + IntToStr(SystemTime.wMonth) + '-' + IntToStr(SystemTime.wDay) + ' ' + IntToStr(SystemTime.wHour) + ':' + IntToStr(SystemTime.wMinute) + ':' + IntToStr(SystemTime.wSecond); //获得需要存储表名称 ReceiveTable := GetTableName; //开始存储数据 ADOQry := TADOQuery.Create(nil); ADOQry.Connection := ADOConn; for m := 0 to (ADOQryTable.RecordCount - 1) do try with ADOQry do begin Close; SQL.Text := 'insert into ' + Trim(ReceiveTable[m]) + ' values(' + IntToStr(NodeID) + ',' + FloatToStr(Pval[m]) + ',' + QuotedStr(SaveDateTime) + ')'; ExecSQL; end; except Exit; end;end;//导出函数exports ConnectServer name 'ConnectSever', SaveData name 'SaveData', SQLSaveData name 'SQLSaveData';beginend.
 
没人能给指出问题出现的原因啊?
 
vc中调用的时候代码如下:void CTttDlg::OnOK() { // TODO: Add extra validation here HINSTANCE hInst = LoadLibrary("c://MySave.dll"); TestDll save; save = (TestDll)GetProcAddress(hInst, "SaveData"); CString aa(save()); CDialog::OnOK();}
 
在loadlibrary时候地址获取不到,请富翁们帮忙找找问题啊
 
发现把ShareMem去掉就行了,但是不明白到底是为什么
 
我试验了下 是可以得到的啊
 
我之前试过一次也可以,后来就死活不能获取,去掉sharmem就好了,就是找不到什么原因。
 
sharemem需要一个DLL文件的,你没把这个文件发布出去,装载MySave.dll就出问题了。
 
哦,我是没有发布那个dll的,呵呵,谢谢富翁们啦
 
多人接受答案了。
 
后退
顶部