I write a C# dll and call it from C++ function. But when i parse a string from C++ to C# function, i will show something weird like this:
d^¡Aè☺
Here is my C++ code:
#include "pch.h"
#include<stdio.h>
#include<Windows.h>
#include<string>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Reflection;
int main()
{
const char* pemodule = ("C:\\Users\\source\\repos\\ClassLibrary2\\ClassLibrary2\\bin\\Debug\\ClassLibrary2.dll");
HMODULE hlib = LoadLibraryA(pemodule);
typedef int(__cdecl *add)(int a, int b);
typedef int(__cdecl* AntiMacro)(std::string path);
auto pAntiMacro = (AntiMacro)GetProcAddress(hlib, "AntiMacro");
std::string path = "C:\\macro\\test";
int a = pAntiMacro(path);
printf("%d\n", a);
return 0;
}
And here is my C# DLL:
[DllExport]
public static int AntiMacro(string filepath)
{
Console.WriteLine(filepath);
bool isFolder = false;
string fullpath = "";
try
{
fullpath = Path.GetFullPath(filepath);
Console.WriteLine(fullpath.ToString());
}
catch (Exception)
{
Console.WriteLine(fullpath);
Console.WriteLine("Invalid path 1");
return 1;
}
I tried to change
std::string path = "C:\\macro\\test"
to
const char *path = "C:\\macro\\test"
But it still not working, is that string in C++ is different from C#?