我有一个用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不是我写的 也不是太明白

解决方案 »

  1.   

    我就是不知道没引用某个对象是什么意思啊
    我还没做任何事情呢  只是OpenPrint()就报这个错了  这里我要引用什么呢
      

  2.   

    [DllImport("printdll.dll")]
    private static extern bool OpenPrint();
    改为:
     [DllImport("printdll.dll", 
          EntryPoint="OpenPrint", 
          CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] 
          public static extern bool OpenPrint();
      

  3.   

    楼主,你原vc写的那个方法是不是static的呢?
      

  4.   

    用了
     kkk_visual(宇宙中我是最厉害的!哈哈(但不包括地球),目前就职) 
    的方法 还是报一样的错误我vc里的方法不是static 是不是不行
      

  5.   

    TO:J_IE_ZH() 可在运行到OpenPrint的时候就报 未将对象引用设置到对象的实例 的错误 怎么回事啊 是不是Dll里面的问题 Dll不是我写的 也不是太明白
      

  6.   

    问题不是在声明上,而是在调用的时候,如下调用:
    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;
      

  7.   

    可是我还没调要到这里来啊
    还在OpenPrint就报错了
      

  8.   

    dll里面不是staic的不行的啦。要new对象才行,所以这个就是报了这个错误。。
      

  9.   

    Sorry!
    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;
      

  10.   

    OpenPrint();应该是一个类的函数,不是一个静态函数,所以你要调用这个函数,要先new对象,这个引用dll应该是要引用整个类才正确了
      

  11.   

    TO:Knight94(愚翁) 这样还是不行呢 还是在OpenPrint()时就出错了
    TO:J_IE_ZH() 
    不是static的 要怎么在调用时new哦
      

  12.   

    不知道在调用Dll时怎么 new个对象呢
    那位大哥告诉哈啊 急
      

  13.   

    这个struct在dll中的原型是怎么样的
      

  14.   

    struct Myprintinfo
    {
    int cmd1;
    int cmd2;
    int cmd3;
    char mystr[1024];
    int mystrlenth;
    };
      

  15.   

    Knight94(愚翁) (
    大哥  要不我把Dll的源代码发给你 你帮我看看 我是在没办法 了
      

  16.   

    你自己把用[DllImport]把dll文件里面的函数调用封装成一个类就可以NEW了哈
      

  17.   

    你要不先把MESSAGEBOX.SHOW加在openprint()后面,看下到时是不是这个报的错
      

  18.   

    还有最好把openprint()在DLL里面的原型粘上来看下
      

  19.   

    [DllImport("printdll.dll",EntryPoint="OpenPrint",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
    public static extern bool OpenPrint();
    试试
      

  20.   

    恩,学习,用c#调用vc写的dll,十个有十个是不能用的,哎,
      

  21.   

    你把dll源代码发往[email protected]我看看。
      

  22.   

    不知道你的dll注册了没?另外,你的dll是不是Com的?
      

  23.   

    昨天早下班了。呵呵。。用vc写dll的时候也有2种情况,一种是其他调用dll的函数,另一种是可以使整个类都被调用,这种情况你就把dll改成后面一种就行了