-
Nov 23rd, 2020, 10:51 AM
#1
Thread Starter
Junior Member
Error when trying to load DLL
I have a DLL that I have created in c++, using codeblocks.
The DLL code is as follows:
Code:
#include "main.h"
#include <stdio.h>
#include <conio.h>
#define TIMEOUT_CONSTANT 50
#define ESC 27
double Get_Position()
{
char outchar = 0;
char inchar = 0;
int byte1=0;
int byte2=0;
int byte3=0;
int byte4=0;
int byte5=0;
int byte6=0;
int byte7=0;
int byte8=0;
double absolute_postion=0;
int loop = 0;
DWORD bytes_written = 0; // Number of bytes written to port
DWORD bytes_read = 0; // Number of bytes read from port
COMMTIMEOUTS tempComTimeouts; // Our temporary time-outs for COM1
COMMTIMEOUTS savedComTimeouts; // Stores the original time-outs
HANDLE comport = NULL; // Handle for COM port
DCB comSettings; // Contains various port settings
// Open COM port
if ((comport =
CreateFile("COM2",
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
_getch();
return(1);
}
// Save time-out parameters for COM
GetCommTimeouts(comport, &savedComTimeouts);
// Set our time-outs
tempComTimeouts.ReadIntervalTimeout = 0;
tempComTimeouts.ReadTotalTimeoutMultiplier = 0;
tempComTimeouts.ReadTotalTimeoutConstant = TIMEOUT_CONSTANT;
tempComTimeouts.WriteTotalTimeoutMultiplier = 0;
tempComTimeouts.WriteTotalTimeoutConstant = TIMEOUT_CONSTANT;
SetCommTimeouts(comport, &tempComTimeouts);
// Set Port parameters.
// We make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then we change the ones we want and call SetCommState().
GetCommState(comport, &comSettings);
comSettings.BaudRate = 2500000;
comSettings.StopBits = ONESTOPBIT;
comSettings.ByteSize = 8;
comSettings.Parity = EVENPARITY;
comSettings.fParity = FALSE;
SetCommState(comport, &comSettings);
bool done = false;
while (!done)
{
if (1)
{
outchar = 0x02;
// Send data. if succesful, WriteFile() returns non-zero
WriteFile(comport, // Handle
&outchar, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);
}
// Read data. if succesful, ReadFile() returns non-zero
ReadFile(comport, // Handle
&inchar, // Incoming data
1, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bytes_read == 1){
switch(loop){
case 0:
break;
case 1:
byte1 = inchar;
if (byte1 < 0){
byte1 = byte1 + 256;
}
break;
case 2:
byte2 = inchar;
if (byte2 < 0){
byte2 = byte2 + 256;
}
break;
case 3:
byte3 = inchar;
if (byte3 < 0){
byte3 = byte3 + 256;
}
break;
case 4:
byte4 = inchar;
if (byte4 < 0){
byte4 = byte4 + 256;
}
break;
case 5:
byte5 = inchar;
if (byte5 < 0){
byte5 = byte5 + 256;
}
break;
case 6:
byte6 = inchar;
if (byte6 < 0){
byte6 = byte6 + 256;
}
break;
case 7:
byte7 = inchar;
if (byte7 < 0){
byte7 = byte7 + 256;
}
break;
case 8:
byte8 = inchar;
if (byte8 < 0){
byte8 = byte8 + 256;
}
done = true;
break;
}
loop++;
}
}
// Restore time-out parameters
SetCommTimeouts(comport, &savedComTimeouts);
CloseHandle(comport);
if (byte4 >= 1){
absolute_postion = ((256 * byte4) + byte3);
absolute_postion = 360 * (absolute_postion / 65536);
}
else{
absolute_postion = byte3;
absolute_postion = 360 * absolute_postion / 65536;
}
return(absolute_postion);
}
and the header is:
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
double Get_Position();
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
in VB.net I call the DLL like this:
Code:
Class DLL_Functions
Public Declare Function Get_Position Lib "C:\Users\Owner\Desktop\Test\test_dll\bin\Debug\test_dll.dll" () As Double
End Class
When I run the function "Get_Position" I get an error "Unable to load DLL. The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
From looking that up the normal fix seems to be either a missing dependency for the DLL, or the DLL not in the correct location. Since I am creating the DLL though, it seems like it has to be something else, I am just not sure what.
-
Nov 23rd, 2020, 01:29 PM
#2
Re: Error when trying to load DLL
are you sure you export Get_Position?
i used to do that in a .def file.
i assume the path "C:\Users\Owner\Desktop\Test\test_dll\bin\Debug\test_dll.dll" is correct. you may want to inspect the dll if it truly exports function Get_Position
-
Nov 23rd, 2020, 01:34 PM
#3
Re: Error when trying to load DLL
-
Nov 23rd, 2020, 02:22 PM
#4
Thread Starter
Junior Member
Re: Error when trying to load DLL
Hmm looks like something else is causing the issue though. I changed the dll export, and it still wasn't working, so I created the most simple DLL ever.
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__
Code:
#include "main.h"
// a sample exported function
int DLL_EXPORT SomeFunction()
{
return 5;
}
I get the same error when I run that. I am thinking there may be something with the way codeblocks is building the DLL that may be the issue.
-
Nov 25th, 2020, 05:17 PM
#5
Re: Error when trying to load DLL
I'm taking a shot in the dark here but this stands out to me:-
C++ Code:
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
It seems that if BUILD_DLL is not defined then DLL_EXPORT will try to import rather than export the function. I'm not sure but I think the compiler's commandline arguments are supposed to take care of defining BUILD_DLL. You might want to check on what commandline options are being used when the compiler is called upon to create the DLL.
You can also try just doing this instead:-
c++ Code:
#define DLL_EXPORT __declspec(dllexport)
Define it without the #ifdef stuff.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|