简体   繁体   中英

Calling a dll function C++ rad studio xe3

I try to call a function from a dll, but it doesn't seem to work fine. Here is the code:

HMODULE  dllhandle;
#include "Unit1.h"
#include <windows.h>
#include <iostream.h>  
#include <conio.h>
void __fastcall TForm1::Button1Click(TObject *Sender)
{

    dllhandle = LoadLibrary((wchar_t*)"PBusDrv.dll");

if(dllhandle)
{
typedef int (*PBUSCONNECTEX)(String aux1, String aux2, String ip);
PBUSCONNECTEX PBusConnectEx;

PBusConnectEx =  (PBUSCONNECTEX)GetProcAddress(dllhandle,  "PBusConnectEx");

PBusConnectEx(" "," ","192.168.30.252");
   }
}

dllhandle keeps returning with a null value.

The problem is probably here:

(wchar_t*)"PBusDrv.dll"

You are casting an ANSI string (1 byte per char) as a wide-string (2 bytes per char). This is never going to work.

You have 3 options:

1- Use the ANSI version of the LoadLibrary function

dllhandle = LoadLibraryA("PBusDrv.dll");

2- Use the appropriate string type according to the project configuration:

dllhandle = LoadLibrary(_T("PBusDrv.dll"));

3- Use the wide-string version of LoadLibrary, while passing a wide-string as input

dllhandle = LoadLibraryW(L"PBusDrv.dll");

Note: Don't mix non-specific function macros with one particular type of string. For example, don't do this:

dllhandle = LoadLibrary(L"PBusDrv.dll");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM