c#调用C++的Dll遇到一系列问题

3 篇文章 0 订阅

一 项目概述

  客户提供了一个使用C++开发生成的Dll库。这个库提供了加密方法,是没有原码的,另外他也提供了使用C++调这个库的demo.可是我们公司都是使用c#的程序员,对C++不是很熟悉。

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
struct GoMem {
	char* StL;
	int Stlen;
	char* EndL;
	int Endlen;
	char* Check;
};
typedef  std::string(*p1)(char*);
typedef  std::string(*p2)(GoMem,char*, char*);
int main(void) {
	HINSTANCE hDllInst;
	GoMem P1;
	GoMem P2;
	GoMem P3;
	P1.StL = (char*)"XRTG030470001A";//起始生产号段
	P1.EndL = (char*)"XRTG030472000A";//结束生产号段
	P1.Stlen = strlen(P1.StL);
	P1.Endlen = strlen(P1.EndL);
	P1.Check = (char*)"2597E";//校验码

	P2.StL = (char*)"12320225641700000";//起始生产号段
	P2.EndL = (char*)"12320225641710000";//结束生产号段
	P2.Stlen = strlen(P2.StL);
	P2.Endlen = strlen(P2.EndL);
	P2.Check = (char*)"0C1C1";//校验码

	P3.StL = (char*)"JB12345000";//起始生产号段
	P3.EndL = (char*)"JB12346000";//结束生产号段
	P3.Stlen = strlen(P3.StL);
	P3.Endlen = strlen(P3.EndL);
	P3.Check = (char*)"17A08";//校验码

	char* Label = (char*)"XRTG030471562A";
	char* Label2 = (char*)"12320225641706520";
	char* Label3 = (char*)"JB12345033";
	char* TID = (char*)"E28069952000400100194DFD";

	hDllInst = LoadLibrary(L"D:\\Work_space\\VCWorkspace\\DllOpen\\EPCProcess.dll");

	p1 EPCDecode = (p1)GetProcAddress(hDllInst, "EPCDecode");
	p2 EncrpyFuc = (p2)GetProcAddress(hDllInst, "EncrpyFuc");
	

	std::string result = EncrpyFuc(P1, Label, TID);//调用动态链接库文件
	std::cout << "Result1:" << result << "\n";

	std::string result2 = EncrpyFuc(P2, Label2, TID);//调用动态链接库文件
	std::cout << "Result2:" << result2 << "\n";

	std::string result3 = EncrpyFuc(P3, Label3, TID);//调用动态链接库文件
	std::cout << "Result3:" << result3 << "\n";

	char* EPC1 = (char*)"88E2308A808695D1D6A384F8";
	char* EPC2 = (char*)"000000012320225641706520";
	char* EPC3 = (char*)"AA004A423132333435303333";

	std::string Label1 = EPCDecode(EPC1);
	std::cout << "Label1:" << Label1 << "\n";

	std::string ExLabel2 = EPCDecode(EPC2);
	std::cout << "Label2:" << ExLabel2 << "\n";

	std::string ExLabel3 = EPCDecode(EPC3);
	std::cout << "Label3:" << ExLabel3 << "\n";

	system("pause");
	return 0;
}

现在创建一个C++ 动态dll库项目,把它这个demo进行改造成一个函数,供外部c#程序调用。

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

struct GoMem {
	char* StL;
	int Stlen;
	char* EndL;
	int Endlen;
	char* Check;
};

typedef  std::string(*p1)(char*);
typedef  std::string(*p2)(GoMem, char*, char*);

extern "C" _declspec(dllexport) char* epcJiaMi(char* wtm, char* tid)
{
	std::cout << "11111:" << "\n";
	HINSTANCE hDllInst;
	GoMem P1;

	P1.StL = (char*)"XRTG030470001A";//起始生产号段
	P1.EndL = (char*)"XRTG030472000A";//结束生产号段
	P1.Stlen = strlen(P1.StL);
	P1.Endlen = strlen(P1.EndL);
	P1.Check = (char*)"2597E";//校验码
	std::cout << "22222:" << "\n";
	//char* Label = wtm;//(char*)"XRTG030471562A";
	//char* TID = tid; //(char*)"E28069952000400100194DFD";

	//hDllInst = LoadLibrary(L"E:\\vscase\\2019\\Study\\CJJTest\\CJJTest\\Debug\\EPCProcess.dll");
	hDllInst = LoadLibrary(L"EPCProcess.dll");
	std::cout << "33333:" << "\n";

	//p1 EPCDecode = (p1)GetProcAddress(hDllInst, "EPCDecode");
	p2 EncrpyFuc = (p2)GetProcAddress(hDllInst, "EncrpyFuc");
	std::cout << "44444:" << "\n";
	//std::string result = EncrpyFuc(P1, Label, TID);//调用动态链接库文件
	std::string result = EncrpyFuc(P1, wtm, tid);//调用动态链接库文件
	std::cout << "Result1:" << result << "\n";

	/*char* EPC1 = (char*)"88E2308A808695D1D6A384F8";
//财哥:2022/12/02, 16:31:34 只能使用下面的方式将string转成char *,使用const_cast<char*>(result.c_str())无法得到正确的值

	std::string Label1 = EPCDecode(EPC1);
	std::cout << "Label1:" << Label1 << "\n";*/
	std::cout << const_cast<char*>(result.c_str()) << "\n"; 
	//system("pause");
	char* result1 = new char[result.length() + 1];
	for (int i = 0; i < result.length(); ++i)
	{
		result1[i] = result[i];
	}
	result1[result.length()] = '\0';
	return result1;//const_cast<char*>(result.c_str());
}

 

【注】平台必须一致。因为客户给的就是32位.

C# 进行调用

 class Program
    {
        //[DllImport("kernel32.dll")]
        //public static extern bool AllocConsole();
        //[DllImport("kernel32.dll")]
        //static extern bool FreeConsole();
        [DllImport("ruiChuDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)]
        //public static extern void epcJiaMi(string wtm, string tid);
        public static extern IntPtr epcJiaMi(IntPtr wtm, IntPtr tid);
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            IntPtr ss = epcJiaMi(Marshal.StringToHGlobalAnsi("XRTG030471562A"), Marshal.StringToHGlobalAnsi("E28069952000400100194DFD"));
            Console.WriteLine("hello2");
            Console.WriteLine(ss);
            Console.WriteLine(Marshal.PtrToStringAnsi(ss));
        }
    }

【总结】1.提示无法写入受保护的内存.....问题。

               因为传参,要使用指针,不能传string类型

              2.提示无法加载编译的dll库.....问题。

                一种情况可能是编译的版本问题,大家都要是32位的。另一情况是少了依赖包

 通过dumpbin来查找依赖

 3.提示 试图加载格式不正确的程序...问题。

虽然把依赖的包都加了进去,又提示这个问题。因为通过everything可以查同名的dll有好几个,搞错了dll,就会报这个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用 C++ DLL 的步骤如下: 1. 在 C++ 中定义一个 DLL 导出函数,该函数需要使用 extern "C" 声明,并使用 __declspec(dllexport) 修饰符导出。该函数的参数和返回值类型需要与 C# 中的声明一致。 2. 在 C# 中声明 DLLImport 属性,用于指定 C++ DLL 的名称和函数签名。 3. 在 C#调用 C++ DLL 中的函数。 下面是一个简单的示例,演示如何在 C#调用 C++ DLLC++ DLL 代码: ```cpp // example.cpp #include "stdafx.h" extern "C" __declspec(dllexport) int add(int a, int b) { return a + b; } ``` C# 代码: ```csharp using System.Runtime.InteropServices; class Program { [DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int add(int a, int b); static void Main(string[] args) { int result = add(1, 2); System.Console.WriteLine(result); } } ``` 在上面的示例中,我们首先在 C++ DLL 中定义了一个名为 add 的函数,并使用 __declspec(dllexport) 修饰符导出。然后我们在 C# 中使用 DllImport 属性指定了 example.dll 的名称和 add 函数的签名。最后,我们在 Main 函数中调用了 add 函数,并将结果打印到控制台上。 需要注意的是,在使用 C++ DLL 时,需要注意函数的调用约定。C++ 默认使用的是 __cdecl 调用约定,而 C# 默认使用的是 __stdcall 调用约定。因此,在使用 C++ DLL 时,需要使用 CallingConvention 属性指定函数的调用约定,以免出现调用错误的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值