我有一个用VC写的操作热敏打印机的Dll
在C#中调用时代码如下:
[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct MYstruct
{
public int cmd1;
public int cmd2;
public int cmd3;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=1024)]
public string mystr;
public int mystrlenth;
};
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport("printdll.dll")]
private static extern bool OpenPrint();
[DllImport("printdll.dll")]
private static extern bool WriteCmd(ref MYstruct myinfo);
[DllImport("printdll.dll")]
private static extern bool Writestring(ref MYstruct myinfo);
[DllImport("printdll.dll")]
private static extern bool Closeprint();
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("1");
OpenPrint();
MYstruct myinfo = new MYstruct();
myinfo.cmd1 = 27;
myinfo.cmd2 = 64;
myinfo.cmd3 = 0;
myinfo.mystr = "小白小白小白小白小白小白小白小白";
myinfo.mystrlenth = myinfo.mystr.Length;
WriteCmd(ref myinfo);
Writestring(ref myinfo);
}可在运行到OpenPrint的时候就报 未将对象引用设置到对象的实例 的错误 怎么回事啊 是不是Dll里面的问题 Dll不是我写的 也不是太明白
在C#中调用时代码如下:
[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct MYstruct
{
public int cmd1;
public int cmd2;
public int cmd3;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=1024)]
public string mystr;
public int mystrlenth;
};
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport("printdll.dll")]
private static extern bool OpenPrint();
[DllImport("printdll.dll")]
private static extern bool WriteCmd(ref MYstruct myinfo);
[DllImport("printdll.dll")]
private static extern bool Writestring(ref MYstruct myinfo);
[DllImport("printdll.dll")]
private static extern bool Closeprint();
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("1");
OpenPrint();
MYstruct myinfo = new MYstruct();
myinfo.cmd1 = 27;
myinfo.cmd2 = 64;
myinfo.cmd3 = 0;
myinfo.mystr = "小白小白小白小白小白小白小白小白";
myinfo.mystrlenth = myinfo.mystr.Length;
WriteCmd(ref myinfo);
Writestring(ref myinfo);
}可在运行到OpenPrint的时候就报 未将对象引用设置到对象的实例 的错误 怎么回事啊 是不是Dll里面的问题 Dll不是我写的 也不是太明白
我还没做任何事情呢 只是OpenPrint()就报这个错了 这里我要引用什么呢
private static extern bool OpenPrint();
改为:
[DllImport("printdll.dll",
EntryPoint="OpenPrint",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern bool OpenPrint();
kkk_visual(宇宙中我是最厉害的!哈哈(但不包括地球),目前就职)
的方法 还是报一样的错误我vc里的方法不是static 是不是不行
MYstruct myinfo = new MYstruct();
myinfo.cmd1 = 27;
myinfo.cmd2 = 64;
myinfo.cmd3 = 0;
myinfo.mystr = "小白小白小白小白小白小白小白小白";
myinfo.mystr.PadRight( 1024, (char)0);//Allocate memory size
myinfo.mystrlenth = myinfo.mystr.Length;
还在OpenPrint就报错了
Have a try!MYstruct myinfo = new MYstruct();
myinfo.cmd1 = 27;
myinfo.cmd2 = 64;
myinfo.cmd3 = 0;
myinfo.mystr = "小白小白小白小白小白小白小白小白";
int nLength = myinfo.mystr.Length;
myinfo.mystr.PadRight( 1024, (char)0);//Allocate memory size
myinfo.mystrlenth = nLength;
TO:J_IE_ZH()
不是static的 要怎么在调用时new哦
那位大哥告诉哈啊 急
{
int cmd1;
int cmd2;
int cmd3;
char mystr[1024];
int mystrlenth;
};
大哥 要不我把Dll的源代码发给你 你帮我看看 我是在没办法 了
public static extern bool OpenPrint();
试试