C
C#17mo ago
exokem

❔ DllImport From C Library

I have compiled a C library to a DLL and am trying to use some of the functions from this library in a C# project. C#:
const string nativeLibName = "FontCache";

[DllImport(nativeLibName, EntryPoint = "FC_CreateFont", CallingConvention = CallingConvention.Cdecl)]
static extern unsafe IntPtr INTERNAL_FC_CreateFont();

[DllImport(nativeLibName, EntryPoint = "FC_LoadFont")]
static extern unsafe int INTERNAL_FC_LoadFont(
IntPtr font,
IntPtr renderer,
byte* filename_ttf,
int pointSize,
SDL_Color color,
int style
);
const string nativeLibName = "FontCache";

[DllImport(nativeLibName, EntryPoint = "FC_CreateFont", CallingConvention = CallingConvention.Cdecl)]
static extern unsafe IntPtr INTERNAL_FC_CreateFont();

[DllImport(nativeLibName, EntryPoint = "FC_LoadFont")]
static extern unsafe int INTERNAL_FC_LoadFont(
IntPtr font,
IntPtr renderer,
byte* filename_ttf,
int pointSize,
SDL_Color color,
int style
);
C:
FC_Font* FC_CreateFont(void);

Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
FC_Font* FC_CreateFont(void);

Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
This exception occurs when either of the above functions are called:
An unhandled exception of type 'System.EntryPointNotFoundException' occurred in Xylem-Media.dll: 'Unable to find an entry point named 'FC_CreateFont' in DLL 'FontCache'.'
An unhandled exception of type 'System.EntryPointNotFoundException' occurred in Xylem-Media.dll: 'Unable to find an entry point named 'FC_CreateFont' in DLL 'FontCache'.'
I don't know if my DllImport signatures are correct, or if there is a problem with the way I have compiled the C library - I have tried using dumpbin and various DLL explorer tools to see which symbols are available but nothing ever appears under the module I am targeting.
9 Replies
exokem
exokemOP17mo ago
the library is pure C I have a wrapper for SDL that works the same way without needing this syntax and adding in the __declspec(dllexport) syntax causes the project not to build
Jester
Jester17mo ago
extern "C"
{
__declspec(dllexport) int __stdcall Example();
}
extern "C"
{
__declspec(dllexport) int __stdcall Example();
}
exokem
exokemOP17mo ago
that syntax is cpp the library is a single C .h file
Jester
Jester17mo ago
<:PES_SadShrug:646092743378206760> thats what i do
exokem
exokemOP17mo ago
it works in C headers? I just don't understand why anything is needed if it is just a C library I thought the point of headers was to expose functions
Jester
Jester17mo ago
i dont know much about it myself but i just know how to write a simple c/c++ dll for my c#
exokem
exokemOP17mo ago
you are definitely right, after some more reading it looks like the __declspec is required because I am not just compiling c, I am compiling c to a DLL and windows always needs extra steps the only point of confusion for me now is why I get a ton of unresolved reference external symbol errors as soon as I mark any symbols to be exported I have determined that this error was because I didn't add the export flag to all symbols
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server