这是我调用的 [DllImport("Zhuzhan.dll", EntryPoint = "IdentityAuthentication", CharSet = CharSet.Ansi)]
static extern int IdentityAuthentication(string Div,byte[] RandAndEndata);
他原始提供的是IdentityAuthentication(char* Div,char* RandAndEndata);
不过经常报错运行时遇到了错误。此错误的地址为 0x62ed646b,在线程 0xf9c 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。
static extern int IdentityAuthentication(string Div,byte[] RandAndEndata);
他原始提供的是IdentityAuthentication(char* Div,char* RandAndEndata);
不过经常报错运行时遇到了错误。此错误的地址为 0x62ed646b,在线程 0xf9c 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>DLLIMPORT void HelloWorld ()
{
MessageBox (0, "Hello!JiaYi Studio!\n", "Hi", MB_ICONINFORMATION);
}DLLIMPORT void OutDir_To_Txt()
{
system("dir >>Dir.txt");/*输出当前目录到Dir文件*/
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break; case DLL_PROCESS_DETACH:
break; case DLL_THREAD_ATTACH:
break; case DLL_THREAD_DETACH:
break;
} /* Returns TRUE on success, FALSE on failure */
return TRUE;
}
/*dll.hJiaYi Studio(yqh2648)*/#ifndef _DLL_H_
#define _DLL_H_#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void HelloWorld (void);
DLLIMPORT void OutDir_To_Txt(void);
#endif /* _DLL_H_ *///C#调用,请将编译好的DLL文件拷贝到C#工程输出目录//嘉怡工作室 湖南省岳阳县荣家湾镇城南村三组//E-Mail:[email protected] 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 WindowsApplication1
{
public partial class Form1 : Form
{
/*[DllImport("MyAdd.dll", SetLastError = true)]
private static extern int Add(int x, int y);
[DllImport("MyAdd.dll", SetLastError = true)]
private static extern IntPtr HelloWorld();
[DllImport("MyAdd.dll", SetLastError = true)]
private static extern char Mystr(char a);*/
[DllImport("OutDir.dll", SetLastError = true)]//我用C语言输出的DLL文件名为OutDir.dlll
private static extern IntPtr OutDir_To_Txt();
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
/* int x = 6;
int y = 9;
int z = Add(x, y);
MessageBox.Show(z.ToString ());*/
} private void button2_Click(object sender, EventArgs e)
{
/* HelloWorld();*/
} private void button3_Click(object sender, EventArgs e)
{
/* char ch = 'c';
char j= Mystr(ch);
MessageBox.Show(j.ToString());*/
} private void button4_Click(object sender, EventArgs e)
{
try
{
OutDir_To_Txt();
MessageBox.Show("输出目录名成功","成功");
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
本示例很简单,只是为了说明怎样调用自己用C语言写的DLL文件,用同样的方法也可调用其它托管DLL文件,如Win API等!
转换成static extern int IdentityAuthentication(string Div,byte[] RandAndEndata);
会报错吗?我的软件是有时报错
public static extern int IdentityAuthentication(string Div,IntPtr RandAndEndata);
public static extern int IdentityAuthentication(string Div, byte[] RandAndEndata);
public int GetIdentityAuthentication(out string randAndEndata)
{
byte[] value = new byte[32];
string Factor="111111";
int index = IdentityAuthentication(Factor, value );
randAndEndata = Encoding.UTF8.GetString(value);
return index;
}
public static extern int IdentityAuthentication(string Div, StringBuilder RandAndEndata);
还是没用
public int GetIdentityAuthentication(out string randAndEndata)
{
byte[] value = new byte[32]; // String sb=new StringBuilder(Factor);
// IntPtr aa = Marshal.AllocHGlobal(value.Length);
// Marshal.Copy(value, 0, aa, value.Length);
StringBuilder aa = new StringBuilder();
aa.Append(value);
int index = IdentityAuthentication(Factor, aa);
randAndEndata = aa.ToString();// Encoding.UTF8.GetString();
GC.Collect();
return index;
}
http://topic.csdn.net/u/20111118/12/1107cce6-84de-4ddc-8c53-ff9d13618d5f.html