C# 调用DLL问题
那位大侠帮我看看这个DLL调用,在Delphi中调用可以正常,在C#中一直出错,delphi的源代码 和dll的函数都附上。还有我用C#写的程序也一起附上,望高手给小弟看看,在C#中如何调用;
Code
USBDRVD_GetDevCount() //获取设备函数
UINT USBDRVD_GetDevCount(IN GUID * DeviceClassGUID); //函数声明
Example:
UINT DeviceCount = USBDRVD_GetDevCount(&GUID);
//一下为DELPHI 调用 可以正常显示
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
_GUID_ = record
l: DWORD;
s1: WORD;
s2: WORD;
c: array[1..8] of BYTE;
end {_GUID_};
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
btnUSB: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
USB_GUID:_GUID_;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
DevCount:LONGINT;
implementation
{$R *.dfm}
function USBDRVD_GetDevCount(const p: _GUID_): Integer; stdcall;
external 'usbdrvd.dll';
procedure TForm1.Timer1Timer(Sender: TObject);
begin
DevCount:= USBDRVD_GetDevCount(USB_GUID);
btnUSB.Caption:= 'USB '+'( '+ inttoStr(DevCount)+' )';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
USB_GUID.L:=$8B021AA5;
USB_GUID.S1:=$A319;
USB_GUID.S2:=$45C1;
USB_GUID.c[1]:=$93;
USB_GUID.c[2]:=$2C;
USB_GUID.c[3]:=$90;
USB_GUID.c[4]:=$AA;
USB_GUID.c[5]:=$82;
USB_GUID.c[6]:=$E6;
USB_GUID.c[7]:=$47;
USB_GUID.c[8]:=$8D;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
end.
///下边为C#调用 提示 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace _5
{
public partial class Form1 : Form
{
public struct UINI
{
public Int64 L;
public Int64 S1;
public Int64 S2;
public Byte[] c;
}
public int DevCount;
public UINI USB_GUID;
public Form1()
{
InitializeComponent();
}
[DllImport("usbdrvd.dll")]
public static extern Int32 USBDRVD_GetDevCount(UINI USB_GUID);
private void button1_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
USB_GUID.c = new Byte[8];
USB_GUID.L=0x8B021AA5;
USB_GUID.S1=0xA319;
USB_GUID.S2=0x45C1;
USB_GUID.c[0]=0x93;
USB_GUID.c[1]=0x2C;
USB_GUID.c[2]=0x90;
USB_GUID.c[3]=0xAA;
USB_GUID.c[4]=0x82;
USB_GUID.c[5]=0xE6;
USB_GUID.c[6]=0x47;
USB_GUID.c[7]=0x8D;
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
DevCount = USBDRVD_GetDevCount(USB_GUID);
btnUSB.Text = "USB " + DevCount;
}
catch
{
btnUSB.Text = "USB " + "no fund";
}
}
}
}
C# 获取不到设备的信息
[DllImport("usbdrvd.dll")]
public static extern Int32 USBDRVD_GetDevCount(UINI USB_GUID);
这块应该怎么调用usbdrvd.dll呢?