C#调用C DLL文件

jacikafx 2011-11-21 11:54:29
这是我调用的 [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 的封送处理错误,这些错误可能会损坏堆栈。

...全文
178 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdl2005lyx 2011-11-24
  • 打赏
  • 举报
回复
sdl2005lyx 2011-11-22
  • 打赏
  • 举报
回复
你把使用这个函数的几行代码贴出来。。。

如果你有C++那么的Demo更好,一并贴出来。。。
jacikafx 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sdl2005lyx 的回复:]
改成:

C# code

[DllImport("Zhuzhan.dll", EntryPoint = "IdentityAuthentication", CharSet = CharSet.Ansi)]
public static extern int IdentityAuthentication(string Div,IntPtr RandAndEndata);


……
[/Quote]


貌似还是偶尔出现这个问题!
jacikafx 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sdl2005lyx 的回复:]
那你这样使用:

C# code

[DllImport("Zhuzhan.dll", EntryPoint = "IdentityAuthentication", CharSet = CharSet.Ansi)]
public static extern int IdentityAuthentication(string Div, StringBuilder RandAndEnda……
[/Quote]

还是没用




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;

}
sdl2005lyx 2011-11-22
  • 打赏
  • 举报
回复
那你这样使用:

[DllImport("Zhuzhan.dll", EntryPoint = "IdentityAuthentication", CharSet = CharSet.Ansi)]
public static extern int IdentityAuthentication(string Div, StringBuilder RandAndEndata);
jacikafx 2011-11-22
  • 打赏
  • 举报
回复
[DllImport("Zhuzhan.dll", EntryPoint = "IdentityAuthentication", CharSet = CharSet.Ansi)]
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;
}
csdn_aspnet 2011-11-21
  • 打赏
  • 举报
回复
/* C语言源代码 (dllmin.c)*/

/* Replace "dll.h" with the name of your header */
#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.h

JiaYi 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:yqh2648@163.com

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 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等!
sdl2005lyx 2011-11-21
  • 打赏
  • 举报
回复
改成:

[DllImport("Zhuzhan.dll", EntryPoint = "IdentityAuthentication", CharSet = CharSet.Ansi)]
public static extern int IdentityAuthentication(string Div,IntPtr RandAndEndata);


jacikafx 2011-11-21
  • 打赏
  • 举报
回复
IdentityAuthentication(char* Div,char* RandAndEndata);
转换成

static extern int IdentityAuthentication(string Div,byte[] RandAndEndata);
会报错吗?我的软件是有时报错

110,553

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧