【发布时间】:2021-06-06 21:01:27
【问题描述】:
我做得对吗?我有两个并行项目,第一个是用 C++ 编写的代码,第二个项目(在 AspNetCore v3.1 中制作的控制台)是尝试调用 C++ 代码中的方法。 我需要在 C# 项目中调用 C++ 方法“解密”。我该怎么做?
C++ 代码
#include <stdlib.h>
#include <string.h>
#include<windows.h>
#include "bascript.hpp"
extern "C"
int FAR PASCAL _export
Decript( const LPSTR name, const LPSTR passwordWithCript,
LPSTR passwordWithoutCript, unsigned int sizeSpaceRetorn ) {
LPSTR result = lpDecript( name, passwordWithCript);
if ( sizeSpaceRetorn < strlen(result) )
return 0;
strcpy( passwordWithoutCript, result );
delete result;
return 1;
}
C#
class Program
{
[DllImport(@"C:\MS\VS\TesteDLLCentura\TesteDLLCentura\bin\Debug\netcoreapp3.1\Sises.DLL", CharSet = CharSet.Auto, EntryPoint = "Decript")]
private static extern string Decript(string name, string passwordWithCript, string passwordWithoutCript, uint sizeSpaceRetorn);
static void Main(string[] args)
{
string retorno = Decript("<user>", "<cript_password>", "", 0);
Console.WriteLine(retorno);
Console.ReadLine();
}
}
【问题讨论】:
-
您是否尝试过使用相对路径? IE。
[DllImport("Sises.dll", CharSet = CharSet.Auto, EntryPoint = "Decript")] -
@Timothy G,不管有没有相对路径,都会出错。 (进程 27840)以代码 -1073740940 退出。
-
您需要声明
StringBuilder passwordWithoutCript,将其预初始化为特定大小,并将该大小传递给sizeSpaceRetorn。这将包含实际结果,以 NUL (0) 字符结尾。返回类型应该是int而不是string
标签: c# c++ asp.net-core console pinvoke