C++封住Dll中的一个方法 :char* checkCardReaderValid (int iPort); 返回值是指针请问在C# 如何接收这个返回值托管和非托管之间如何传送   我是这样写的
[DllImport("idcarddll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]        
        [return: MarshalAs(UnmanagedType.LPStr)]
        static extern IntPtr checkCardReaderValid(int iPort);
        //功能:检查读卡器是否可以正常工作。
        //启动读卡       
        private void button7_Click(object sender, EventArgs e)
        {            
            IntPtr lpByte =checkCardReaderValid(0);
            if (lpByte != IntPtr.Zero)
            {
                string result = Marshal.PtrToStringAnsi(lpByte);
            }
            else
            {
                throw new Exception("返回字符串为空");
            }
        }
报错如下:无法封送处理“return value”: 无效的托管/非托管类型组合

解决方案 »

  1.   

    StringBuilder 和string 都不行的
      

  2.   

      [return: MarshalAs(UnmanagedType.LPStr)]
    //去掉试试;
      

  3.   

    返回到IntPtr然后用
    Marshal.Copy(IntPtr source,
    char[] destination,
    int startIndex,
    int length
    ) 复制
      

  4.   

    不是高手,也进来了..baidu了一下,你这个问题发了很久了,好像现在还没解决.
    想问一下,c#里好像可以直接调用unmanaged code啊..你为什么要使用marshal呢?
    为什么不直接调用c++的代码呢?我用过几次marshal,但只在访问com对象的时候用的.其它的情况我不太清楚..
      

  5.   

    unmanaged code 是啥   没C++源码  只有sdk(dll)我的设备是读取二代身份证芯片里面的信息
      

  6.   

    marshal 是要考虑,不过好像.net已经帮你考虑过了,所以自己不需要再考虑了..
    我做了一个试验:
    //c++ code; dll functions;
    #include <stdio.h>
    #include "string.h"extern "C" __declspec(dllexport)  
    char*  TestFunc(int i)
    {
    char * retValue= new char[100];
    switch (i)
    {
    case  0:
              strcpy(retValue,"you have inputed 0"); break;
    case  1:
      strcpy(retValue,"you have inputed 1"); break;
    case  2:
    strcpy(retValue,"you have inputed 2"); break;
    default: break;
    }
        
    return retValue;

    }//c# code; consumer;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;namespace CSharpConsumer
    {
        class Program
        {
            [DllImport("testLib.dll", EntryPoint = "TestFunc", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl,
                SetLastError = true)]
            static extern string TestFunc(int index);        static void Main(string[] args)
            {           
                Console.WriteLine(TestFunc(1));
             
            }
        }
    }
    ==================================
    运行结果:you have inputed 1
      

  7.   

    The static methods defined on the Marshal class are essential to working with unmanaged code. Most methods defined in this class are typically used by developers who want to provide a bridge between the managed and unmanaged programming models. For example, the StringToHGlobalAnsi method copies ANSI characters from a specified string (in the managed heap) to a buffer in the unmanaged heap. It also allocates the target heap of the right size.The common language runtime provides two mechanisms for interoperating with unmanaged code:Platform invoke, which enables managed code to call functions exported from an unmanaged library.COM interop, which enables managed code to interact with Component Object Model (COM) objects through interfaces.Both platform invoke and COM interop use interop marshaling to accurately move method arguments between caller and callee and back, if required. As the following illustration shows, a platform invoke method call flows from managed to unmanaged code and never the other way, except when callback functions are involved. Even though platform invoke calls can flow only from managed to unmanaged code, data can flow in both directions as input or output parameters. COM interop method calls can flow in either direction. 
      

  8.   

    One of the tasks performed by the P-Invoke service is to marshal data across the managed/unmanaged code boundary. The data types used by DLL functions rarely have the same names as the CLR data types, so it is incumbent on the programmer to determine the correct CLR data types to use. The table below lists the commonly use data types in unmanaged C functions and in the Windows API along with the CLR equivalents.Unmanaged Windows API types Unmanaged C language types CLR type 
    HANDLE void* IntPtr 
    BYTE unsigned char Byte 
    SHORT short Int16 
    WORD unsigned short UInt16 
    INT, LONG int, long Int32 
    BOOL int, long Int32 or Boolean 
    UINT unsigned int UInt32 
    DWORD, ULONG unsigned long UInt32 
    CHAR char Char 
    LPSTR, LPCSTR, LPWSTR, LPCWSTR char*, wchar_t* String or StringBuilder 
    FLOAT float Single 
    DOUBLE double Double 
    别轻易用marshel,CLR能做的,就让CLR帮你做...
      

  9.   

    不行啊  老兄  用string 和StringBuilder  虽然没报错 但是返回值为空啊
      

  10.   

    怎么可能?
    你运行我的例子了吗?
    为什么我的代码能返回值?你的c++代码有问题吗?你用C++调你的函数,看看返回值是不是为空总结一下: 1.在c++里验证你的export的函数能否正常工作;
             2.不需要自己marshal,所以你的代码要改一改.我的sample code可以正常工作
      

  11.   

    可以转换成delegate委托的形式 进行调用
    委托就是相当于指针吗 。。可以去搜搜相关例子