Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应该为c#中的指针char*传递什么参数? 我有一个C++编写的第三方DLL,我使用了 dLimPult< /C> >,_C#_C++_Pointers_Parameters_Dllimport - Fatal编程技术网

应该为c#中的指针char*传递什么参数? 我有一个C++编写的第三方DLL,我使用了 dLimPult< /C> >,

应该为c#中的指针char*传递什么参数? 我有一个C++编写的第三方DLL,我使用了 dLimPult< /C> >,,c#,c++,pointers,parameters,dllimport,C#,C++,Pointers,Parameters,Dllimport,他们还提供了调用函数的文档。有些函数很容易处理,没有传递任何参数。现在我不知道如何用c#中传递的指针参数调用函数,如下图所示 这是我的密码 [DllImport(third-parth.dll")] public static extern int ReadInfo( ref StringBuilder MagInfo, ref StringBuilder TicketNumber, string BmpFileNamePath, int PicFormat);

他们还提供了调用函数的文档。有些函数很容易处理,没有传递任何参数。现在我不知道如何用c#中传递的指针参数调用函数,如下图所示

这是我的密码

 [DllImport(third-parth.dll")]
    public static extern int ReadInfo( ref StringBuilder MagInfo, ref StringBuilder TicketNumber,  string BmpFileNamePath, int PicFormat);


    public int read_info()
    {
       StringBuilder Mag = new StringBuilder(100);
        string path = "folder";
        return ReadInfo( ref Mag, ref Mag, path,1);


    }
我在网上看到了这个例子

编辑:

我还尝试了两种通话约定

我在一个C++文件中发现了这一点,有助于解决这个问题。

typedef int (__stdcall *ReadInfo)(char* MagInfo, char* TicketNumber, char* BmpFileNamePath, int PicFormat);
编辑:2

这里是如何在C++演示中调用函数

 int CScanChequeDllDemo_VCDlg::ReadImage_BackSide()
{
    //*************************************************************
    //Read back side image data status, user can read back side image.  
    if(Global_DevStatus.iStatus == SCANCHEQUE_STATUS_SCAN_REVERSED)
    {
        //*************************************************************
        //have image data
        if(Global_DevStatus.iHaveData != 0)
        {
            memset(Global_MagInfo, 0x00, sizeof(Global_MagInfo));
            memset(Global_TicketNumber, 0x00, sizeof(Global_TicketNumber));
            memset(Global_BmpFileNamePath, 0x00, sizeof(Global_BmpFileNamePath));

            //*************************************************************
            //Read back side image data
            Global_returnValue = U_ReadInfo(Global_MagInfo, Global_TicketNumber,Global_BmpFileNamePath, Global_PicFormat);

            //refresh picture
            ShowImage(this,NULL);

            //show status
            ShowStatus(Global_returnValue); 
        
            if(Global_returnValue == SCANCHEQUE_ERROR_WRITEFILE)
            {
                //wait for instruction from the application software
            }
            
            //*************************************************************
            //Get device status 
            Global_returnValue = U_QueryStatus(&Global_DevStatus);
            if(Global_returnValue != SCANCHEQUE_SUCCESS)
            {
                ShowStatus(Global_returnValue);
                return -1;
            }
        }
    }

    return 0;
}
这是全局变量

char                            Global_MagInfo[1024]            = {0x00};                   
char                            Global_TicketNumber[1024]       = {0x00};                   
char                            Global_BmpFileNamePath[1024]    = {0x00};                   
int                             Global_PicFormat                = (IMAGE_FORMAT_BMP | IMAGE_FORMAT_JPG | IMAGE_FORMAT_TIFF);//reversed

我可以看到两个错误。您可能需要分配两个
StringBuilder
实例,如注释中所述。而且
StringBuilder
参数不应具有
ref

调用约定可能是错误的。我们无法确定问题中显示的代码应该是什么


我还建议明确说明字符集,尽管默认值是ANSI,因此这不会是一个正确性问题。

回答我自己的问题可能会对某人有所帮助

  [DllImport(@yourDLL", EntryPoint ="ReadInfo", CharSet = CharSet.Ansi)]
    public static extern int ReadInfo(IntPtr MagInfo, IntPtr TicketNumber,  IntPtr BmpFileNamePath,int picFormat);

    public int read_info()
    {

        
        IntPtr m = Marshal.AllocHGlobal(1024);
        IntPtr t = Marshal.AllocHGlobal(1024);
        IntPtr p = Marshal.AllocHGlobal(1024);


        int result = ReadInfo(m, t, p, 0);

        string mginfo = Marshal.PtrToStringAnsi(m);
        string tinfo = Marshal.PtrToStringAnsi(t);
        string pth = Marshal.PtrToStringAnsi(p);

        Console.WriteLine(mginfo);
        Console.WriteLine(pth);
       
        Marshal.FreeHGlobal(m);
        Marshal.FreeHGlobal(t);
        Marshal.FreeHGlobal(p);
        return result;
     

    }

尝试将字符集指定为
CharSet.Ansi
,(C中的字符总是8位宽)-看看示例,看看他们是如何修饰DllImport属性的。另一个是不需要通过引用传递字符串生成器。请尝试不使用ref修饰符。@BlueStrat在修改代码“未知模块中发生“System.NullReferenceException”类型的未处理异常。对象引用未设置为对象的实例”后出现此错误。我注意到您正在将同一StringBuilder实例传递给MagInfo和TicketNumber参数。您可以尝试创建两个单独的实例,每个参数一个吗?@BlueStrat已经尝试过这些,但仍然没有结果。我有一个编辑问题,我认为可以帮助它超越一旦你删除两个REF关键字,C ^匹配的C++ Type。我不知道“仍然没有”是什么意思。我得到
未知模块中发生了类型为“System.NullReferenceException”的未处理异常。对象引用未设置为对象的实例。
这似乎是由我们看不见的东西引起的。我不知道我在这里缺少了什么。我有所有的演示+文档。但我是NoOB,涉及到指针和C++。无论如何,我已经用一个调用
ReadInfo()
  [DllImport(@yourDLL", EntryPoint ="ReadInfo", CharSet = CharSet.Ansi)]
    public static extern int ReadInfo(IntPtr MagInfo, IntPtr TicketNumber,  IntPtr BmpFileNamePath,int picFormat);

    public int read_info()
    {

        
        IntPtr m = Marshal.AllocHGlobal(1024);
        IntPtr t = Marshal.AllocHGlobal(1024);
        IntPtr p = Marshal.AllocHGlobal(1024);


        int result = ReadInfo(m, t, p, 0);

        string mginfo = Marshal.PtrToStringAnsi(m);
        string tinfo = Marshal.PtrToStringAnsi(t);
        string pth = Marshal.PtrToStringAnsi(p);

        Console.WriteLine(mginfo);
        Console.WriteLine(pth);
       
        Marshal.FreeHGlobal(m);
        Marshal.FreeHGlobal(t);
        Marshal.FreeHGlobal(p);
        return result;
     

    }