C#调用VC编写的DLL(给分)

zhangyze 2004-08-04 04:37:05
新手提问,用vc封装了一个dll,但是在c#中调用时,dll只得到一个字符。请高手给与解答,谢谢!!
这段是我用vc写的dll的程序
//Export.cpp
include "stdafx.h"
#include "AppApi.h"
static CString GetTempName(CString strPath);

BOOL WINAPI OnSend(CString strNumber)
{
AfxMessageBox(strNumber);
return true;
}

//ApppApi.h
#ifdef __cplusplus
extern "C" {
#endif
/* __cplusplus */


struct SmsData
{
BOOL bSmsFullScreen;
char szSmsPath[MAX_PATH]; // no unicode path support
char szSmsFilename[MAX_PATH];
};

BOOL WINAPI OnSend(CString strNumber);

#ifdef __cplusplus
}
#endif

下面是c#的程序
//

[DllImport("Sms.dll", EntryPoint="OnSend",CharSet=CharSet.Auto)]
public static extern int OnSend(string phone);
private void button1_Click(object sender, System.EventArgs e)
{
string AA;
AA=textBox1.Text;
try
{
Form1.OnSend(AA);
}
catch( EntryPointNotFoundException)
{
}

}





谢谢各位大虾了
...全文
187 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangyze 2004-08-05
  • 打赏
  • 举报
回复
谢谢,终于搞定了,感谢各位大虾
zhangyze 2004-08-05
  • 打赏
  • 举报
回复
为什么采用上述方法依然无法将字符串全部传递过去呢?
谢谢了
jojopoper 2004-08-04
  • 打赏
  • 举报
回复
写错了,应该是OnSend(char* pNumber);
jojopoper 2004-08-04
  • 打赏
  • 举报
回复
把你接口改成char*的OnSend(char* *pNumber);
[DllImport("Sms.dll",EntryPoint="OnSend")]
public static extern void OnSend( char[] Number);
然后
private void button1_Click(object sender, System.EventArgs e)
{
string AA;
AA=textBox1.Text;
try
{
Form1.OnSend(AA.ToCharArray());
}
catch( EntryPointNotFoundException)
{
}

}

应该就OK了
Knight94 2004-08-04
  • 打赏
  • 举报
回复
不过建议不要用CString,用char*可能更好些
Knight94 2004-08-04
  • 打赏
  • 举报
回复
把string改成StringBuilder试试

110,535

社区成员

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

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

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