3F / DllExport

.NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat)
MIT License
968 stars 133 forks source link

Can this tool create .h & .lib files correspond to the C# function signature? #60

Closed BlueSolei closed 4 years ago

BlueSolei commented 6 years ago

Question: Can this tool create a .h file with the C++ function's signature? Can it create a .lib file, so I can statically link to this DLL and won't need to do all this dynamic loading LoadLibraryEx\GetProcAdddress boilerplate error-prone code?

3F commented 6 years ago

Can this tool create a .h file with the C++ function's signature?

You can get and format the list of exported functions as you need through -pe-exp-list (v1.6-RC)

https://github.com/3F/DllExport/issues/55#issuecomment-339989014

DllExport -pe-exp-list bin\Debug\regXwild.dll

But without full signatures. It easy to implement for .net environment via Cecil etc., but for unmanaged C++ we need to implement translation to equivalents for marshaled types, and so on.

Can it create a .lib file, so I can statically link to this DLL and won't need to do all this dynamic loading

You can generate additional .exp & .lib when $(DllExportGenExpLib = true) (for Wizard see same option "Generate .exp + .lib ...") but of course only to reference exported definitions.

MyInit -> DllExportClassLibrary1.dll

00000AA0  4C 01 0A D8 32 5A 23 00 00 00 01 00 08 00 5F 4D  L..Ш2Z#......._M
00000AB0  79 49 6E 69 74 00 44 6C 6C 45 78 70 6F 72 74 43  yInit.DllExportC
00000AC0  6C 61 73 73 4C 69 62 72 61 72 79 31 2E 64 6C 6C  lassLibrary1.dll
00000AD0  00 0A 2F 30 20 20 20 20 20 20 20 20 20 20 20 20  ../0            
BlueSolei commented 6 years ago

So how should I know what is the unmanaged c++ counterpart to an exported C# function? My problem is that it seems I need to guess the signature. Obviously, the DllExport knows what the right signature, as it creates an exported unmanaged function to serve as a proxy to the C# function, IIUC. For example, if the C# signature is:

[DllExport]
public static void print(string str) {}

I guessed that if I call it as:

void print(const char* str) {}

it will work. And fortunately, it worked :-) But when I wish to find the unmanaged signature of:

[DllExport]
public static void printArr(string[] str) {}

I didn't find the right unmanaged signature :-( Do I miss something on how to use this tool?

3F commented 6 years ago

@BlueSolei Only primitive types and read more about PInvoke.

Related:

That is, use pointer to allocated string etc.

[DllExport]
public static void print(IntPtr ptr) {}
3F commented 6 years ago

I updated solutions for how to use complex types and strings between unmanaged and managed environments:

But going back to the original question from this issue!

if this was not a misunderstanding and if it really is needed to someone, well, please clarify additional details. Probably I can implement something if this will be justified for some cases.