紧急求助,如何用c调用delphi写得dll!!(在线等!!!解决就结帖)

hoverqjl 2003-12-08 01:51:54
各位大虾,我现在需要用c或c++调用一个delphi写得dll,不知道如何调用,请大家指导一下,最好给出个例子,感激不尽!!
我得dll的源码如下,是解压和压缩的:
library ZIPDLL;


uses
ShareMem,
SysUtils,
Classes,
Windows,
ZLib;

{$R *.res}

function TestDll(i:integer):integer;stdcall;
begin
Result:=i+1;
end;

procedure CompressStream(SourceStream, DestStream: TStream);
var
CompressionStream: TCompressionStream;
Count: Integer;
begin
Count := SourceStream.Size;
DestStream.WriteBuffer(Count, SizeOf(Count));
CompressionStream := TCompressionStream.Create(clDefault, DestStream);
try
CompressionStream.CopyFrom(SourceStream, SourceStream.Size);
finally
CompressionStream.Free;
end;
end;

procedure UnCompressStream(SourceStream, DestStream: TStream);
var
DecompressionStream: TDecompressionStream;
Count: Integer;
begin
SourceStream.ReadBuffer(Count, SizeOf(Count));
DecompressionStream := TDecompressionStream.Create(SourceStream);
try
DestStream.CopyFrom(DecompressionStream, Count);
finally
DecompressionStream.Free;
end;
end;

procedure ZipFile(SFileName,DFileName: String);stdcall;
var
SourceStream, DestStream: TFileStream;
begin

SourceStream := TFileStream.Create(SFileName, fmOpenRead or fmShareDenyWrite);
DestStream := TFileStream.Create(DFileName, fmCreate);
try
CompressStream(SourceStream, DestStream);
FlushFileBuffers(DestStream.Handle);

finally
DestStream.Free;
SourceStream.Free;
end;
end;

procedure UnzipFile(SFileName,DFileName: String);stdcall;
var
SourceStream, DestStream: TFileStream;
begin

SourceStream := TFileStream.Create(SFileName, fmOpenRead or fmShareDenyWrite);
DestStream := TFileStream.Create(DFileName, fmCreate);
try
UnCompressStream(SourceStream, DestStream);
FlushFileBuffers(DestStream.Handle);

finally
DestStream.Free;
SourceStream.Free;
end;
end;


exports
testdll,
ZipFile,
UnzipFile;
begin


end.
...全文
25 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hoverqjl 2003-12-08
呵呵,错误找到了,不好意思,zipfile=(_ZIPFILE)GetProcAddress(hLibrary,"ZipFile");
应该为zipfile=(_ZIPFILE)GetProcAddress(hLibrary,"ZipFile");,那个括号写错了,程序可以运行了,但还是有如下提示
An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x0
Function=[Unknown.]
Library=(N/A)

NOTE: We are unable to locate the function name symbol for the error
just occurred. Please refer to release documentation for possible
reason and solutions.


Current Java thread:
at login.userlogin.vertifyid(Native Method)
at login.userlogin.main(userlogin.java:46)

Dynamic libraries:
0x00400000 - 0x00406000 C:\WINNT\system32\java.exe
0x77F80000 - 0x77FFB000 C:\WINNT\system32\ntdll.dll
0x796D0000 - 0x79732000 C:\WINNT\system32\ADVAPI32.dll
0x77E60000 - 0x77F32000 C:\WINNT\system32\KERNEL32.DLL
0x786F0000 - 0x7875E000 C:\WINNT\system32\RPCRT4.DLL
0x78000000 - 0x78045000 C:\WINNT\system32\MSVCRT.dll
0x6D340000 - 0x6D46A000 D:\JBuilder9\jdk1.4\jre\bin\client\jvm.dll
0x77DF0000 - 0x77E4F000 C:\WINNT\system32\USER32.dll
0x77F40000 - 0x77F79000 C:\WINNT\system32\GDI32.dll
0x77530000 - 0x77560000 C:\WINNT\system32\WINMM.dll
0x75E00000 - 0x75E1A000 C:\WINNT\system32\IMM32.DLL
0x6C330000 - 0x6C338000 C:\WINNT\system32\LPK.DLL
0x65D20000 - 0x65D74000 C:\WINNT\system32\USP10.dll
0x10000000 - 0x10009000 C:\WINNT\system32\apihookdll.dll
0x6D1E0000 - 0x6D1E7000 D:\JBuilder9\jdk1.4\jre\bin\hpi.dll
0x6D310000 - 0x6D31E000 D:\JBuilder9\jdk1.4\jre\bin\verify.dll
0x6D220000 - 0x6D239000 D:\JBuilder9\jdk1.4\jre\bin\java.dll
0x6D330000 - 0x6D33D000 D:\JBuilder9\jdk1.4\jre\bin\zip.dll
0x0AD70000 - 0x0AD77000 D:\qjl\dll\login_userlogin.dll
0x0AE90000 - 0x0AEBD000 D:\qjl\dll\ZIPDLL.dll
0x77990000 - 0x77A2B000 C:\WINNT\system32\oleaut32.dll
0x77A30000 - 0x77B1C000 C:\WINNT\system32\ole32.dll
0x41000000 - 0x4100C000 C:\WINNT\system32\borlndmm.dll
0x77900000 - 0x77923000 C:\WINNT\system32\imagehlp.dll
0x72960000 - 0x7298D000 C:\WINNT\system32\DBGHELP.dll
0x687E0000 - 0x687EB000 C:\WINNT\system32\PSAPI.DLL

Local Time = Mon Dec 08 16:05:26 2003
Elapsed Time = 271
#
# The exception above was detected in native code outside the VM
#
# Java VM: Java HotSpot(TM) Client VM (1.4.1_02-b06 mixed mode)
#
# An error report file has been saved as hs_err_pid2292.log.
# Please refer to the file for further information.
#

这什么原因呢?
回复
hoverqjl 2003-12-08
这是我写的调用dll的,但出了许多错阿

#include "windows.h"
#include "stdio.h"
#include "login_userlogin.h"

typedef void (FAR WINAPI * _ZIPFILE)(char*,char*);
typedef void (FAR WINAPI * _UNZIPFILE)(char*,char*);

JNIEXPORT jint JNICALL Java_login_userlogin_vertifyid
(JNIEnv *env, jobject obj_this)
{ const char* cuserid;
const char* cpassword;

int tag;
jstring juserid;
jstring jpassword;
jclass class_userlogin;
jfieldID id_userid;
jfieldID id_pass;

HINSTANCE hLibrary=NULL;
_ZIPFILE zipfile;
_UNZIPFILE unzipfile;

class_userlogin=(*env)->GetObjectClass(env,obj_this);
id_userid=(*env)->GetFieldID(env,class_userlogin,"userid","Ljava/lang/String;");
juserid=(*env)->GetObjectField(env,obj_this,id_userid);
cuserid=(*env)->GetStringUTFChars(env,juserid,NULL);

class_userlogin=(*env)->GetObjectClass(env,obj_this);
id_pass=(*env)->GetFieldID(env,class_userlogin,"password","Ljava/lang/String;");
jpassword=(*env)->GetObjectField(env,obj_this,id_pass);
cpassword=(*env)->GetStringUTFChars(env,jpassword,NULL);

hLibrary=LoadLibrary("ZIPDLL.dll");

zipfile=(_ZIPFILE)GetProcAddress(hLibrary,"ZipFile");
unzipfile=(_UNZIPFILE)GetProcAddress(hLibrary,"UnZipFile");

(*zipfile)(cuserid,cpassword);
(*unzipfile)("tttt.z","tttt.1");


FreeLibrary(hLibrary);
return 1;

}

错误如下:
ogin_userlogin.c(36) : error C2018: unknown character '0xa3'
login_userlogin.c(36) : error C2018: unknown character '0xa8'
login_userlogin.c(36) : error C2059: syntax error : ')'
login_userlogin.c(36) : error C2275: '_ZIPFILE' : illegal use of this type as an
expression
login_userlogin.c(5) : see declaration of '_ZIPFILE'
login_userlogin.c(36) : error C2146: syntax error : missing ';' before identifie
r 'GetProcAddress'
login_userlogin.c(39) : warning C4090: 'function' : different 'const' qualifiers

login_userlogin.c(39) : warning C4024: 'zipfile' : different types for formal an
d actual parameter 1
login_userlogin.c(39) : warning C4090: 'function' : different 'const' qualifiers

login_userlogin.c(39) : warning C4024: 'zipfile' : different types for formal an
d actual parameter 2

错误都集中在这一行
zipfile=(_ZIPFILE)GetProcAddress(hLibrary,"ZipFile");
该怎么改呢???
回复
hoverqjl 2003-12-08
谢谢犬哥了,那我的zip.dll里封装了delphi里的许多类,比如SourceStream, DestStream: TStream,这些不用管吗?你能再说详细一点吗?谢了!我对c,delphi都不懂,我最终是要用java调用这个dll,但java只能调用c的,所以的先用c调!
回复
liuleilover 2003-12-08
首先定义这些函数的指针定义

typedef int (*TESTDLL)(int);
typedef void (*ZIPFILE)(char*,char*);
typedef void (*UNZIPFILE)(char*,char*);

然后用LoadLibrary 和 GetProcAddress 函数。

HANDLE hDll=LoadLibrary("zipdll.dll");
TESTDLL TestDll=(TESTDLL)GetProcAddress(hDll,"TestDll");
ZIPFILE ZipFile=(ZIPFILE)GetProcAddress(hDll,"ZipFile");
UNZIPFILE UnzipFile=(UNZIPFILE)GetProcAddress(hDll,"UnZipFile");

调用时形如:
TestDll(1);
ZipFile("tttt.1","tttt.z");
UnzipFile("tttt.z","tttt.1");

具体细节自己做吧!!!

回复
9731boy 2003-12-08
在delphi问问呢>?
回复
相关推荐
发帖
C语言
创建于2007-09-28

6.4w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2003-12-08 01:51
社区公告
暂无公告