我对C++可以说是一窍不通,我现在要调用别人写得API,所以不得不想办法调用了请知道的兄弟们给帮帮忙啊!C++函数:(只截取了程序的一部分出来)#define    OUT
#define    IN
typedef          void         *HDISK;//---------------------------------export--------------------------extern "C" 
{
//only support in win2000 and later OS version
//get handle  
//return value: if success,return handle of disk
//              if fail, return NULL 
HDISK  __stdcall InitDiskByLetter(       
CHAR cDiskLetter //Letter of disk
); BOOL __stdcall GetProductInfo( 
HDISK   IN   hDriver,     //handle of USB Drive
PCHAR OUT  pVendorName, // point to the buffer that receive vendor name, >=8 bytes
PCHAR OUT  pProductName, // point to the buffer that receive product name, >=16 bytes
PWORD     OUT   pVID,    //point to the WORD that to vendor ID
PWORD     OUT   pPID    //point to the WORD that to product ID
); //close a handle of USB Drive
void   __stdcall   CloseDeviceHandle(HDISK   IN   hDriver); /////////////////////////////////////////
目前需要用C#调用这三个函数,HDISK是什么类型的变量,该怎么调用,请大家帮助,谢谢!

解决方案 »

  1.   

    http://download.csdn.net/source/2439376
    这是C#调用c++ dll的实例。
    我也遇到同样的项目。
    现在这个难题由其他人解决。
    下面给你一段我曾经尝试过的代码:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using System.Runtime.InteropServices;public partial class _Default : System.Web.UI.Page
    {
        [DllImport(@"D:\Backup\我的文档\Visual Studio 2005\WebSites\CppDll\Bin\WinDll.dll")]
        public static extern int Sum(int x, int y);    [DllImport(@"D:\Backup\我的文档\Visual Studio 2005\WebSites\CppDll\Bin\MFCDLL.dll")]
        public static extern char Hello();    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(Hello());
            Response.Write(Sum(3, 4).ToString());
        }    public class DllInvoke
        {
            [DllImport("kernel32.dll")]
            private extern static IntPtr LoadLibrary(String path);
            [DllImport("kernel32.dll")]
            private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
            [DllImport("kernel32.dll")]
            private extern static bool FreeLibrary(IntPtr lib);
            private IntPtr hLib;
            public DllInvoke(String DLLPath)
            {
                hLib = LoadLibrary(DLLPath);
            }
            ~DllInvoke()
            {
                FreeLibrary(hLib);
            }
            //将要执行的函数转换为委托
            public Delegate Invoke(String APIName, Type t)
            {
                IntPtr api = GetProcAddress(hLib, APIName);
                return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
            }
        }    private void UseDll()
        { 
            //public delegate int Compile(String command, StringBuilder inf);//编译
            //DllInvoke dll = new DllInvoke(Server.MapPath(@"~/Bin/Judge.dll"));
            //Compile compile = (Compile)dll.Invoke("Compile", typeof(Compile));
            //StringBuilder inf;
            //compile(@“gcc a.c -o a.exe“,inf); //这里就是调用我的DLL里定义的Compile函数 
        }
    }
      

  2.   

    我也遇到过!不过后来我逼着他们把C++的DLL重新写成了C#的DLL 哈哈
      

  3.   

    platform invoke
    如3楼的
    对照好类型即可
      

  4.   

    [DllImport("yourdll.dll",CallingConvention=CallingConvention.StdCall)]
    HandleRef InitDiskByLetter(char cDiskLetter); [DllImport("yourdll.dll",CallingConvention=CallingConvention.StdCall)]
    bool GetProductInfo( 
    HandleRef hDriver, //handle of USB Drive
    string pVendorName, // point to the buffer that receive vendor name, >=8 bytes
    string pProductName, // point to the buffer that receive product name, >=16 bytes
    ref uint pVID, //point to the WORD that to vendor ID
    ref uint pPID //point to the WORD that to product ID
    );[DllImport("yourdll.dll",CallingConvention=CallingConvention.StdCall)]
    void CloseDeviceHandle(HandleRef hDriver);这样声明即可使用,你是在web工程中使用,需要注意dll的路径。