C
C#3mo ago
Silme94

✅ Using a C# DLL in C++

im actually trying to call a C# dll from c++ with DllExport, the c++ code return no error but dont execute the functions, here is the source : C# :
using System.Runtime.InteropServices;
using System;


namespace InternalTest
{
public class Class1
{

[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);


[DllExport(CallingConvention = CallingConvention.StdCall)]
public static void DllEntryPoint()
{
Console.WriteLine("This is a message from C#");
MessageBox(0, "C# is good", "hello world", 0);
}
}
}
using System.Runtime.InteropServices;
using System;


namespace InternalTest
{
public class Class1
{

[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);


[DllExport(CallingConvention = CallingConvention.StdCall)]
public static void DllEntryPoint()
{
Console.WriteLine("This is a message from C#");
MessageBox(0, "C# is good", "hello world", 0);
}
}
}
C++ :
#include <stdio.h>
#include <Windows.h>

int main(int argc, char **argv) {
if (argv[1] == NULL) {
printf("Usage : dll_executor <DLL_PATH>\n");
return -1;
}

char* Dll_Path = argv[1];
HMODULE hModule = LoadLibraryA(Dll_Path);
if (hModule == NULL) {
printf("Failed to load the dll.\n");
}

LPCSTR Function_To_Execute = "DllEntryPoint"; // Function to execute in the dll

HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, Function_To_Execute), NULL, 0, NULL);
if (hThread == NULL) {
printf("Failed to create thread.\n");
}

printf("Dll Executed!\n");
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
FreeLibrary(hModule);

return 0;
}
#include <stdio.h>
#include <Windows.h>

int main(int argc, char **argv) {
if (argv[1] == NULL) {
printf("Usage : dll_executor <DLL_PATH>\n");
return -1;
}

char* Dll_Path = argv[1];
HMODULE hModule = LoadLibraryA(Dll_Path);
if (hModule == NULL) {
printf("Failed to load the dll.\n");
}

LPCSTR Function_To_Execute = "DllEntryPoint"; // Function to execute in the dll

HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, Function_To_Execute), NULL, 0, NULL);
if (hThread == NULL) {
printf("Failed to create thread.\n");
}

printf("Dll Executed!\n");
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
FreeLibrary(hModule);

return 0;
}
The cpp code says that it got executed but it doesn't really
29 Replies
Jimmacle
Jimmacle3mo ago
you can't do this this way, C# isn't compiled to native code there's a whole runtime you need to set up first
Silme94
Silme943mo ago
how do i compile to native code
Jimmacle
Jimmacle3mo ago
you can't, not in a way that will work for this what's your actual goal?
Silme94
Silme943mo ago
just to make a C# library for C++
Jimmacle
Jimmacle3mo ago
but why
Jimmacle
Jimmacle3mo ago
i mean, if you really want to this is a starting point https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting
Write a custom .NET runtime host - .NET
Learn to host the .NET runtime from native code to support advanced scenarios that require controlling how the .NET runtime works.
leowest
leowest3mo ago
GitHub
Quick start
.NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat) - 3F/DllExport
arion
arion3mo ago
You could make a native aot library https://luxeengine.com/csharp-to-native-library/
Silme94
Silme943mo ago
it does nothing when i run the code
#include <stdio.h>
#include <Windows.h>

typedef void(__cdecl *DllEntryPoint)();

int main(int argc, char **argv) {

HMODULE lib = LoadLibrary("InternalTest.dll");
if (lib == NULL) {
printf("Failed to load the dll.\n");
}

auto func = (DllEntryPoint)GetProcAddress(lib, "DllEntryPoint");
func();

printf("Dll Executed!\n");

FreeLibrary(lib);

return 0;
}
#include <stdio.h>
#include <Windows.h>

typedef void(__cdecl *DllEntryPoint)();

int main(int argc, char **argv) {

HMODULE lib = LoadLibrary("InternalTest.dll");
if (lib == NULL) {
printf("Failed to load the dll.\n");
}

auto func = (DllEntryPoint)GetProcAddress(lib, "DllEntryPoint");
func();

printf("Dll Executed!\n");

FreeLibrary(lib);

return 0;
}
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace InternalTest
{
public class Class1
{

[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);

[DllExport(CallingConvention = CallingConvention.StdCall)]
static void DllEntryPoint()
{
Console.WriteLine("This is a message from C#");
MessageBox(0, "C# is good", "hello world", 0);
}
}
}
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace InternalTest
{
public class Class1
{

[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);

[DllExport(CallingConvention = CallingConvention.StdCall)]
static void DllEntryPoint()
{
Console.WriteLine("This is a message from C#");
MessageBox(0, "C# is good", "hello world", 0);
}
}
}
it dont even print "Dll Executed!" thanks for ur response, ill try that
leowest
leowest3mo ago
well that doesn't look like its using that library it has videos and abunch of step by steps u can look into
Silme94
Silme943mo ago
it stops when calling the function
Loading dll...
lib=c3540000...
Executing Dll...
Loading dll...
lib=c3540000...
Executing Dll...
#include <stdio.h>
#include <Windows.h>

typedef void(__cdecl *DllEntryPoint)();

int main(int argc, char **argv) {
printf("Loading dll...\n");
HMODULE lib = LoadLibrary("InternalTest.dll");
if (lib == NULL) {
printf("Failed to load the dll.\n");
}
printf("lib=%x...\n", lib);
auto func = (DllEntryPoint)GetProcAddress(lib, "DllEntryPoint");
printf("Executing Dll...\n");
func();

printf("Dll Executed!\n");

FreeLibrary(lib);

return 0;
}
#include <stdio.h>
#include <Windows.h>

typedef void(__cdecl *DllEntryPoint)();

int main(int argc, char **argv) {
printf("Loading dll...\n");
HMODULE lib = LoadLibrary("InternalTest.dll");
if (lib == NULL) {
printf("Failed to load the dll.\n");
}
printf("lib=%x...\n", lib);
auto func = (DllEntryPoint)GetProcAddress(lib, "DllEntryPoint");
printf("Executing Dll...\n");
func();

printf("Dll Executed!\n");

FreeLibrary(lib);

return 0;
}
leowest
leowest3mo ago
yeah but your code is not using the library from waht u showed...
Silme94
Silme943mo ago
wdym
leowest
leowest3mo ago
I linked u a library, with their quick start on how to use it and ur just quoting me on not working with some code that doesn't look like what the library provides
Silme94
Silme943mo ago
i clicker on " C++ ❤ C# (+📹)" the link at the right
leowest
leowest3mo ago
its not just code u need to use the library u understand that right?
Silme94
Silme943mo ago
?
Silme94
Silme943mo ago
it dont work
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace InternalTest
{
public class Class1
{

[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);

[UnmanagedCallersOnly(EntryPoint = "hello_world")]
static void DllEntryPoint()
{
Console.WriteLine("This is a message from C#");
MessageBox(0, "C# is good", "hello world", 0);
}
}
}
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace InternalTest
{
public class Class1
{

[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);

[UnmanagedCallersOnly(EntryPoint = "hello_world")]
static void DllEntryPoint()
{
Console.WriteLine("This is a message from C#");
MessageBox(0, "C# is good", "hello world", 0);
}
}
}
#include <stdio.h>
#include <Windows.h>

typedef void (*VoidFN)();

int main(int argc, char **argv) {
void* library = LoadLibraryA("InternalTest.dll");
VoidFN fn = (VoidFN)GetProcAddress((HMODULE)library, "hello_world");
fn();
return 0;
}
#include <stdio.h>
#include <Windows.h>

typedef void (*VoidFN)();

int main(int argc, char **argv) {
void* library = LoadLibraryA("InternalTest.dll");
VoidFN fn = (VoidFN)GetProcAddress((HMODULE)library, "hello_world");
fn();
return 0;
}
oh this is working for u
arion
arion3mo ago
did you build it like the article said?
Silme94
Silme943mo ago
yeah, i added <PublishAOT> to true but this is the same thing as compiling in vs code ? tbh i compiled with vs i will try that
arion
arion3mo ago
publishing is not the same as compiling
Silme94
Silme943mo ago
yeah my bad
arion
arion3mo ago
just remember, its in your publish folder (mine in the screenshot is in C:\Users\Dawn\RiderProjects\NAOTTest\NAOTTest\bin\Release\net7.0\win-x64\publish)
Silme94
Silme943mo ago
@ bro thank you this is working well thanks a lot
arion
arion3mo ago
:okHand: just remember, native aot (c# native) has some drawbacks, from the top of my head um, reflection is a nono, Json serialization gets harder, and some other things, i forgor though i could be wrong, im a whole version of .net behind on native aot, they might have gotten better already
Silme94
Silme943mo ago
/solved !solved bruh
arion
arion3mo ago
i think its /close or smthn