静态链接库文件封装成DLL后调用问题,高手请进!!!

cnwhm 2003-12-19 12:52:43
在我的项目中要将第三方RSA的认证集成到项目中,该认证产品提供有SDK包。由静态链接文件(ace_api.lib)

和头文件(apiuser.h)组成。但在C#中只可调用动态链接库,所以在VC中将其封装。

函数原型:
extern int Sd_ApiInit(char *serverDb,char *logDb,char *mode,char *msgBuf,int bufSize);

具体如下:
1、在项目中静态引用lib文件。
2、将apiuser.h文件加入到HeadFiles中。
3、在主程序中敲入下面的代码:
#include "apiuser.h"
//声明apiuser.h中要封装的函数:
int ApiInit(char *serverDb,char *logDb,char *mode,char *msgBuf,int bufSize)
{
int result=Sd_ApiInit(serverDb,logDb,mode,msgBuf,bufSize);
return result;
}
......
......
4、在项目的.def文件中写入要封装的函数:
EXPORTS
ApiInit @1
...... @2
...... @2
5、编译。生成win32dll.dll。
这下,所需的动态链接库文件就生成了。可在C#中使用了。
将编译成功的dll复制至项目输出文件夹下(bin\Debug目录下)
在项目中就可调用了。
代码如下
using System.Runtime.InteropServices
......
[DllImport("win32dll")]
private static extern int ApiInit(string serdb,string logdb,string defser,string msgbuf,int

bufSize);
.....
//调用
pivate void button1_OnClick(object sender,System.EventArgs e)
{
string msg="";
int result=ApiInit("","","1",msg,4096);
MessageBox.Show(result.toString());
......
}

编译执行,执行后单击按钮后程序直接就退出了。但将传递的参数bufSize的值改为其它数值,如ApiInit("","","1",msg,12),会弹出错误代码为1,调用正确,因为传递了错误的参数,但传递正确的参数为何报错?

1、是不是我封装的动态链接库文件有问题?
2、还是函数引用不对?
请高手指点!!!

下面是SDK开发包中带的开发示例中引用这个函数的一部分代码:
#ifndef MAX_RESULT_MSG_SIZE
#define MAX_RESULT_MSG_SIZE 4096 /* Should be in all your applications!!! */
#endif
......
char buffer[MAX_RESULT_MSG_SIZE] = {0};
......
......
error = Sd_ApiInit("","","1", buffer, MAX_RESULT_MSG_SIZE);


下面是函数的使用说明:
...全文
129 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnwhm 2003-12-25
  • 打赏
  • 举报
回复
[DllImport("win32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.Winapi)]
将Winapi改为StdApi即可
cnwhm 2003-12-23
  • 打赏
  • 举报
回复
自己解决了!散分!

这么好的帖子没人顶,哎!
cnwhm 2003-12-20
  • 打赏
  • 举报
回复
现在问题基本已确定。是转换后的DLL在C#中调用的数据类型造成的问题,下面是我改后的代码,但仍有错误出现:(
[DllImport("win32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.Winapi)]
private static extern int ApiRev(string msgBuf,int bufSize);

调用:
char signchar=(char)10000;
string msg=new String(signchar,255);
int error=ApiRev(msg,4096);
MessageBox.Show(error.ToString());

调用成功,但显示的是乱码。而CharSet字符集设定中的每一项我都试过了。有些可以用,也是乱码,有些就象下面说的会弹出错误。而同样的方法去调用ApiInit会出现“应用程序中发生了未处理的异常......未将对象引用设置到对象的实例。。”的错误。
用StringBuidler也是同样的错误。

CSDN上的相关的帖子我都看了,还没找到解决的办法。

高手快来啊!
大家帮忙顶啊!


cnwhm 2003-12-19
  • 打赏
  • 举报
回复

C Function Prototype
extern int Sd_ApiInit(char *serverDb, char *logDb, char *mode, char *msgBuf, int bufSize);

Synopsis
Initializes and connects to the RSA ACE/Server databases in multi-user mode.

Description
Initializes and connects to the Server databases in multi-user mode. This function must be
called before any other function that performs database operations. To connect successfully,
the user must be an RSA ACE/Server administrator. The database commit mode is always
“automatic,” meaning that each API function call is a separate database transaction.

Input Arguments: C

serverDB Path and filename of the Server database file. May be passed as an
empty string if the VAR_ACE environmental variable is set
correctly (see page 21).

logDB Path and filename of the Log database file. May be passed as an
empty string if the VAR_ACE environmental variable is set
correctly(see page 21).

mode Any nonzero value for automatic commit mode. Manual commit
mode (value 0) is not supported in the current version and causes an
error.


Return Values
Values returned are OK (value 0) upon successful completion of function or ERROR
(value 1) if an error condition exists.

Return Text
Connected User: UserName on Host: HostName

Logged Events
SDADMIN_UNAUTHORIZED or SDADMIN_STAR
tommyhuanglei 2003-12-19
  • 打赏
  • 举报
回复
帮你顶,我也正在做呢!
顺便mark
我的是调用错误,我没有程序的代码?是vc编的dll
qq:22443800
email & msn :tommyhuanglei@hotmail.com
等待高手帮忙!
dashi 2003-12-19
  • 打赏
  • 举报
回复
顶,分给偶,不要给斑竹
webdiyer 2003-12-19
  • 打赏
  • 举报
回复
呵呵,谁叫他坐我旁边呢?我不顶谁顶!!:)
速马 2003-12-19
  • 打赏
  • 举报
回复
版主在骗分 (-:
webdiyer 2003-12-19
  • 打赏
  • 举报
回复
帮你顶一下:)
cnwhm 2003-12-19
  • 打赏
  • 举报
回复
大侠们去哪儿了?帮帮我吧!
都调试了一天了,还没头绪。
到底是我的DLL封装的有问题,还是在C#中调用的有问题,还是函数的使用上的问题?给点经验或建议。
谢谢了!

dldl 2003-12-19
  • 打赏
  • 举报
回复
mark

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧