[求助] C# 调用非托管DLL的问题

wangdali 2005-05-25 03:26:56
厂商提供了一个非托管的DLL库
原形如下:
extern "C" {
__declspec(dllexport) BOOL SendSM(char*, char*, char*, char*, char*);
}

我是这样调用的:
using System.Runtime.InteropServices;

[DllImport("SPConnector.dll", EntryPoint="SendSM",ExactSpelling=false,SetLastError=true,CharSet = CharSet.Ansi)]
public static extern int SendSM(String a, String b, String c, String d, String e);

// 调用
SendSM("111", "111", "111", "111", "111");

=========================================
总是提示:未将对象引用设置到对象的实例。
请高手帮忙!!!

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参阅此消息的结尾。

************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
at SmsGate.Form1.SendSM(String a, String b, String c, String d, String e)
at SmsGate.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.2300
基本代码: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
SmsGate
程序集版本: 1.0.1971.24921
Win32 版本: 1.0.1971.24921
基本代码: file:///F:/vctest/SmsGate/bin/Release/SmsGate.exe
----------------------------------------
System.Windows.Forms
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.2300
基本代码: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.2300
基本代码: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.2300
基本代码: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
mscorlib.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
----------------------------------------

************** JIT 调试 **************
计算机的配置文件(machine.config)的
system.windows.forms 节中必须设置 jitDebugging 值。
编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n


...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimh 2005-05-25
  • 打赏
  • 举报
回复
api定义有问题,应该是这样的
public static extern int SendSM(
[MarshalAs(UnmanagedType.LPTStr)]String a,
[MarshalAs(UnmanagedType.LPTStr)]String b,
[MarshalAs(UnmanagedType.LPTStr)]String c,
[MarshalAs(UnmanagedType.LPTStr)]String d,
[MarshalAs(UnmanagedType.LPTStr)]String e);
wangdali 2005-05-25
  • 打赏
  • 举报
回复
to ybh2002(冰鸿) :
你说的没有初始化是指
SendSM()方法没有初始化呢?
还是 SendSM()中的char* 没有初始化呢?
wangdali 2005-05-25
  • 打赏
  • 举报
回复
谁能给我一个初始化的具体代码?
谢谢
ybh2002 2005-05-25
  • 打赏
  • 举报
回复
对象有没初始化?
wangdali 2005-05-25
  • 打赏
  • 举报
回复
__declspec(dllexport) BOOL SendSM(char*, char*, char*, char*, char*);

这里所有的char* 都是不返回的,只有dll处理这些输入成功后才返回BOOL 判断是否处理成功.
fellowcheng 2005-05-25
  • 打赏
  • 举报
回复
你所调用函数的参数中是否有一个或多个是用来传递返回值的

110,534

社区成员

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

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

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