Search code examples
c#visual-studio-2013dllexport

Create a DLL from a .lib file


This question is based on the following post: https://stackoverflow.com/users/9999861/blackbriar

Now I have the problem that everytime I want to use the dll a System.EntryPointNotFoundException occurs. The message of the exception says, that the entry point with the name of the function I tried to call, was not found in my dll.

Here is an example function that is located in the .h file:

#ifdef __cplusplus
extern "C"
{
#endif
__declspec(dllexport) int __stdcall LDL_Init(char* code)
...
#ifdef __cplusplus
}
#endif

And I imported the function in C# like this:

[DllImport("C:\\Path\\To\\Dll\\Example.dll", EntryPoint="LDL_Init", CallingConvention=CallingConvention.StdCall)]

public static extern int LDL_Init( [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder code );

Does someone have an idea what I am doing wrong?

Next Try:

I analyzed the generated dll with Dependency Walker and recognized that no function was exported. So I Wrote a wrapper-class. Here the new code examples:

In Library.h:

int LDL_Init(char* code);

In LibraryWrapper.h:

#pragma once

    class __declspec(dllexport) LibraryWrapper
    {
    public:
        static int __cdecl LDL_Init(char* code);
    };

In LibraryWrapper.cpp.

#include "Library.h"
#include "LibraryWrapper.h"

int LibraryWrapper::LDL_Init(char* code){
    return LDL_Init(code);
}

In Library.cs:

[DllImport("C:\\Path\\To\\Dll\\Example.dll")]
public static extern int LDL_Init( [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder code );

Sadly I have the same result at the execution of the program: The good old System.EntryPointNotFoundException...

Here a screenshot of the result of Dependency Walker: Screenshot of Dependency walker

And without undecorating C++ methods: Dependency walker screenshot without undecorating


Solution

  • I "solved" the problem with the following adjustment in the file Library.cs:

    [DllImport("C:\\Path\\To\\Dll\\Example.dll", EntryPoint="?LDL_Init@LibraryWrapper@@SAHPEAD@Z")]
    public static extern int LDL_Init( [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder code );
    

    The string EntryPoint is based on the function name that Dependency Walker extracted from my dll. Afterwards I had the problem, that the execution of the code stood still at the line

    return LDL_init(code);
    

    In the file LibraryWrapper.cpp. Found this out by enabling "native code debugging" and pressing pause while debugging.

    In the meantime I found a .dll that was provided by the producer of the device I want to control. I analyzed the .dll with Dependency Walker and inserted the EntryPoints. Now it works. Thanks for all the support.