当前位置:数据分析 > vs2010 c dll环境设置_vs2010环境中C#调用C++ dll时常见的三个问题(内存损坏、托管调试...

vs2010 c dll环境设置_vs2010环境中C#调用C++ dll时常见的三个问题(内存损坏、托管调试...

  • 发布:2023-09-30 23:01

来源:VS2010环境下使用ANSI C创建DLL及使用方法

1。创建DLL项目

1.1开始VS 2010

1.2 创建dll工程。

操作:文件->新建->项目->Win32 控制台应用程序。

b.输入项目名称,这里我们使用dll,点击确定按钮。

c.点击下一步,在“应用程序设置界面设置”中勾选DLL(D)项和空项,点击完成按钮。

d。 View->Solution Explorer,右键“头文件”,Add->New Item,这里我们使用dll.h

右键“Source File”,Add->New Item,这里我们添加dll.c,dll工程就完成了。

1.3 dll.h内容如下

#ifndef AXLPLUGIN_H

#define AXLPLUGIN_H

/**/

#ifdef _WINDOWS

#define DLL_DECLARE __declspec(dllexport)

#其他

#define DLL_DECLARE

#endif

DLL_DECLARE int Min(int a, int b);

/* 在此列出所有函数声明 */

#endif

1.4 dll.c内容如下

#include "dll.h"

#包括

/*根据需要添加相应的头文件*/

DLL_DECLARE int Min(int a, int b)

{

如果 (a >= b)

返回b;

其他

返回a;

}

/*在这里实现所有声明的函数*/

2。 DLL文件的使用

2.1经测试证明dll.dll文件应与dll.lib、dll.h文件一起使用

2.2 新建一个“Win32应用程序”用于测试

操作:文件->新建->项目->Win32 控制台应用程序。

b.输入项目名称,这里我们使用test_dll,然后点击确定按钮。

c.点击下一步,在“应用程序设置界面”中设置“控制台应用程序”和“空项目”,然后点击完成按钮。

d。将工程dll目录下的dll.h/dll.dll/dll.lib复制到工程test_dll目录下。

View->Solution Explorer,右键“头文件”,Add->New Item,这里我们使用dll.h

右键“源文件”,Add->New Item,这里我们添加test_dll.c,右键“资源文件”,

添加->“现有项”,选择dll.lib,至此test_dll工程就完成了。

2.3 编辑test_dll.c文件,内容如下

#include "dll.h"

#包括

int main()

{

printf("最小值(2, 4) = %d/n", 最小值(2, 4));

printf("最小值(5, 2) = %d/n", 最小值(5, 2));

返回0;

}

2.4 dll和test_dll项目的目录结构

2.4.1 dll工程目录结构

../dll/

│dll.sdf

│dll.sln

│dll_dir.txt

├─调试

│dll.dll

│dll.exp

│dll.ilk

│dll.lib

│ dll.pdb

├─dll

│ │ dll.cpp

│ │ dll.h

│ │ dll.vcxproj

│ │ dll.vcxproj.filters

│ │ dll.vcxproj.user

│ └─调试

│ cl.command.1.tlog

│ CL.read.1.tlog

│ CL.write.1.tlog

│ dll.Build.CppClean.log

│ dll.dll.embed.manifest

│ dll.dll.embed.manifest.res

│ dll.dll.intermediate.manifest

│ dll.lastbuildstate

│dll.log

│dll.obj

│dll.write.1.tlog

│dll_manifest.rc

│link-cvtres.read.1.tlog

│link-cvtres.write.1.tlog

│ link.2360-cvtres.read.1.tlog

│ link.2360-cvtres.write.1.tlog

│链接.2360.读.1.离开

│链接.2360.write.1.tlog

│ link.command.1.exit

│ link.read.1.leave

│ link.write.1.tlog

│ mt.command.1.exit

│ mt.read.1.leave

│ mt.write.1.exit

│rc.command.1.exit

│ rc.read.1.leave

│rc.write.1.exit

│RCa02504

│vc100.idb

│vc100.pdb

└─ipch

└─dll-75fa4624

dll-129995a8.ipch

2.4.2 test_dll 是默认设置

../test_dll/

│test_dll.sdf

│test_dll.sln

│test_dll_dir.txt

├─调试

│ test_dll.exe

│test_dll.ilk

│ test_dll.pdb

├─ipch

│ └─test_dll-eb5063a1

│ test_dll-c06c53e7.ipch

└─test_dll

│ dll.dll

│ dll.h

│ dll.lib

│ test_dll.c

│ test_dll.vcxproj

│ test_dll.vcxproj.filters

│ test_dll.vcxproj.user

└─调试

cl.command.1.退出

www.sychzs.cn.1.离开

CL.write.1.退出

link-cvtres.read.1.leave

link-cvtres.write.1.tlog

链接.3004-cvtres.read.1.tlog

链接.3004-cvtres.write.1.tlog

链接.www.sychzs.cn.1.退出

链接.3004.write.1.tlog

link.command.1.exit

链接.读取.1.离开

link.write.1.tlog

mt.command.1.leave

mt.read.1.leave

mt.write.1.exit

rc.command.1.exit

rc.read.1.leave

rc.write.1.退出

test_dll.Build.CppClean.log

test_dll.exe.embed.manifest

test_dll.exe.embed.manifest.res

test_dll.exe.intermediate.manifest

test_dll.lastbuildstate

test_dll.log

test_dll.obj

test_dll_manifest.rc

vc100.idb

vc100.pdb

问题1:C# 尝试读取或写入受保护的内存,这通常表明其他内存已损坏。 System.AccessViolationException

解决方案1:dll工程中的函数声明:CPPDLLEXPORT_API void bayes(char* inFileName, float* Tex, int &classifier);

c#工程中的函数声明如下: [DllImport(@"..........\cppdllexport.dll", EntryPoint = "bayes")] ? ? ? ? extern static int bayes(string inFileName , Single[] Tex, int 分类器);

解决办法:去掉dll函数声明中的引用符号。也许 C# 不支持引用或其他原因。

问题2:发生 PInvokeStackImbalance 消息:托管调试助手“PInvokeStackImbalance”检测到“C:\...\csdllimport.exe”中存在问题。附加信息:对 PInvoke 函数“....”的调用导致堆栈不对称。原因可能是托管的“PInvoke”签名与非托管目标签名不匹配。请检查“PInvoke”的调用约定和参数是否正确。签名与非托管目标签名匹配。

解决方案2:修改C#中的配置,解决方案视图---》项目----》属性-----》应用程序-----》目标框架,从.Net 4.0更改为.Net 3.5够了

问题3:我们遇到的是项目中的实际问题。需要调试C++ dll函数。在vs2010环境下,大致调试步骤如下:

a。在“解决方案视图”中找到需要调试的dll项目,右键将该dll设置为调试启动项目;然后选择dll工程打开属性页设置页面;

b。在属性页中,选择配置属性下拉菜单,然后选择调试;

c。在调试页面,选择要启动的调试器的下拉菜单中,有本地调试和远程调试选项;

d。在调试页面中,选择命令中的下拉菜单,找到引用该dll的应用程序;在命令参数中输入$(TargetFileName),或者自己从下拉框中选择,然后在该字符串后面输入dll 文件导出的函数名称:

最终形式为:$(TargetFileName) bayestrain;贝叶斯测试

工作目录下拉菜单,选择当前工作目录,即“.”。

大致步骤截图如下:

如何将dll设置为另一个.exe应用程序

1。创建DLL文件

1 选择vs2010下的win32应用程序,创建DLL工程

2 创建头文件testdll.h

#ifndef TestDll_H_

#define TestDll_H_

#ifdef MYLIBDLL

#define MYLIBDLL extern "C" _declspec(dllimport)

#其他

#define MYLIBDLL extern "C" _declspec(dllexport)

#endif

MYLIBDLL int Add(int plus1, int plus2);

//也可以这样写:

//外部“C”{

//_declspec(dllexport) int Add(int plus1, int plus2);

//};

#endif

3 创建源文件

#include“stdafx.h”

#include "testdll.h"

#包括

使用命名空间 std;

int Add(int plus1, int plus2)

{

int add_result = plus1 + plus2;

返回add_result;

}4 创建模块文件

库“MyDLL”

出口

添加@15 选择要编译的release版本

2。调用DLL文件中的函数

#include“stdafx.h”

#包括

#包括

#pragma comment(lib,“MyDll.lib”)

extern "C"_declspec (dllimport) int Add(int plus1, int plus2);

int _tmain(int argc, _TCHAR* argv[])

{

printf("%d\n",添加(6,4));

返回0;

}

相关文章