请教关于调用DLL出错问题.
我调用一VC写的DLL出现如下错误:
Project Project15.exe raised exception class EAccessViolation with message 'Access violation at address 01123371 in module 'phoneDriver.dll'. Write of address 010F2B72'. Process stopped. Use Step or Run to continue.
说明:
phoneDriver.dll 为一连接硬件的DLL,出厂的时候附带的。封装是非标准形式,用的是导出类的形式。为了使该DLL能给其他语言掉用,我将其用标准形式封装成 VC_UPhone.dll,封装方法是新建一个生成dll的工程(原来的是生成.exe的工程),将原来的除界面外的代码复制过去,写一.h文件(加上了.def文件)。封装后已试验过其中有2个函数可以给C++Builder和Delphi调用。但是在调用startPlayFile函数时出现以上问题。
源代码如下:
unit Unit14;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{function callOut(callId: string; stateSign: boolean):integer;stdcall;}
function startPlayFile(filePath: pchar; openPlay: boolean; volume: longint; replay: boolean): boolean;stdcall;
external 'VC_UPhone.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
startPlayFile('E:\123.WAV',true,100,false);
end;
end.
该函数用以下C++Builder代码证实应该是可以调得出来,只是用不了:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
////---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
////第一步:函数定义。这里的函数为地址转换函数。下面这个函数其实就是定义bool __stdcall startPlayFile(char *filePath, bool openPlay,long volume,bool replay);
bool __stdcall (*startPlayFile)(char *filePath, bool openPlay,long volume,bool replay);
////第二步:定义模块句柄,全局变量,它是载入DLL文件后的实例
HINSTANCE HmyDLL;
////---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
////第三步:装入DLL文件,同时获得它的句柄
HmyDLL=LoadLibrary("VC_UPhone.dll");
}
////---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
////第四步:定义函数地址变量
FARPROC P;
if(HmyDLL!=NULL)
{
////第五步:获取动态链接库内的某一函数的内存地址
P=GetProcAddress(HmyDLL,"startPlayFile");
if(P==NULL)
{
ShowMessage("打开startPlayFile()函数错误!");
}
else
{
////第六步:强制类型转换,即将所获取的函数地址强制转换为函数
startPlayFile=(bool__stdcall (__cdecl *)())P;
////第七步:函数调用
startPlayFile('E:\123.WAV',true,100,false);
}
}
else
{
ShowMessage("打开动态链接库文件VC_UPhone.dll错误!");
}
}
没出现两个错误应该就是说调用DLL和函数成功了吧?
另外,该函数用Delphi调用也出现类似错误:
Project Project14.exe raised exception class EAccessViolation with message 'Access violation at address 00383371 in module 'phoneDriver.dll'. Write of address 004281BC'. Process stopped. Use Step or Run to continue.
是不是我封装出来的"VC_UPhone.dll"有些函数用到,但某些函数是用不到呢?有兼容性问题?
求教各位大侠问题所在,十分感谢!

