Author Topic: Issue creating DLL  (Read 4445 times)

Offline hopslam

  • Single posting newcomer
  • *
  • Posts: 3
Issue creating DLL
« on: November 23, 2020, 09:27:10 pm »
I am trying to create a DLL in codeblocks, and use it in VB.net, but I have been having issues getting the DLL to work. I have created a very simple DLL to test with, and wondering if anyone would know of any settings or anything else that may be causing the issue.

The cpp file:
Code
#include "main.h"

// a sample exported function
int DLL_EXPORT SomeFunction()
{
    return 5;
}

The header file:
Code
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>
/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
#endif

int DLL_EXPORT SomeFunction();

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

I call the function into VB.net like this:
Code
Class DLL_Functions
    Public Declare Function SomeFunction Lib "C:\Users\Owner\Desktop\Header Test\test\bin\Debug\test.dll" () As Integer
End Class

When I go to run the function I get an error: "Unable to load DLL. The specified module could not be found. (Exception from HRESULT: 0x8007007E)"

I have checked just to ensure the file path is correct as well.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Issue creating DLL
« Reply #1 on: November 23, 2020, 11:44:09 pm »
Post the full rebuild log.
Can you use depends.exe to inspect your dll?
Do you see the function?
My guess is that C++ name mangling is causing you the trouble.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline hopslam

  • Single posting newcomer
  • *
  • Posts: 3
Re: Issue creating DLL
« Reply #2 on: November 30, 2020, 05:58:18 pm »
Post the full rebuild log.
Can you use depends.exe to inspect your dll?
Do you see the function?
My guess is that C++ name mangling is causing you the trouble.

Sorry I somehow missed the reply to the post.

The full build log is:

-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -DBUILD_DLL -g  -c "C:\Users\Owner\Desktop\Header Test\test\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -shared -Wl,--output-def=bin\Debug\libtest.def -Wl,--out-implib=bin\Debug\libtest.a -Wl,--dll  obj\Debug\main.o  -o bin\Debug\test.dll  -luser32
Output file is bin\Debug\test.dll with size 35.87 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

When I try to use dependency walker I get an error: At least one required implicit or forwarded dependency was not found, and a warning: At least one delay-load dependency module was not found. The problem with that is every API-MS_WIN_CORE dll gives an error.

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: Issue creating DLL
« Reply #3 on: November 30, 2020, 06:30:57 pm »
Dependency Walker is kind of broken since Windows 7, it has lots of trouble with this new API related stuff. A better alternative is to use https://github.com/lucasg/Dependencies, it can resolve these dependencies properly.

Offline hopslam

  • Single posting newcomer
  • *
  • Posts: 3
Re: Issue creating DLL
« Reply #4 on: November 30, 2020, 08:33:11 pm »
Ok so that worked a lot better. When using the test DLL, it gives says it cannot find libgcc_s_dw2-1.dll.

Edit: After searching real fast it it seems like just enabling static libgcc and static libstdc++ fixed the issue. Are there any other general settings that are usually good to have enabled?
« Last Edit: November 30, 2020, 08:42:49 pm by hopslam »

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: Issue creating DLL
« Reply #5 on: November 30, 2020, 11:17:57 pm »
Although you are on windows i wouldn't statically link the compiler runtime libraries, this might cause trouble. I would simply distribute them together with the application.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Issue creating DLL
« Reply #6 on: December 01, 2020, 01:58:02 am »
Edit: After searching real fast it it seems like just enabling static libgcc and static libstdc++ fixed the issue. Are there any other general settings that are usually good to have enabled?
You should first check what are the licensing requirements of the libraries you're using.
Generally GCC and its runtime are GPL licensed (with some exception clauses).
You should be pretty sure that you understand the license and the license allows you to do what you want to do.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]