我对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是什么类型的变量,该怎么调用,请大家帮助,谢谢!
#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是什么类型的变量,该怎么调用,请大家帮助,谢谢!
解决方案 »
- 如何用JavaScript 这个效果
- 想做一个类似于这个网站的同城购物站,大家看得出用的是哪种源码吗?
- 使用由强名称生成的密钥文件
- 这个正则怎么写???
- 附加自动命名的数据库,但失败。已存在同名的数据库,或指定的文件无法打开或位于 UNC 共享目录中。
- 请推荐几个免费支持asp.net服务器,数据库是SQL2005。或者是几天试用的也可以!
- 如何用JS调用aspx页面,在线给分。。
- Session_end的timeout跳转问题!大哥们指点下,50分!
- 如何实现:点击页面中的按钮,就可以保存该页面?
- 哪有visual studio .net 下载呀?
- 怎么去掉fck图片的表单提交
- 各位老大,有没有用javascript实现gridview二次查询的例子呀
这是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函数
}
}
如3楼的
对照好类型即可
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的路径。