victorteoh90
victorteoh90

Reputation: 5

It "stopped working" when i run this code. I wish to call this simple function from the API

I just want to call the function from the API. WhozCraig just now told me that mine is just a type and not a function. But I still don't know how to solve this. How should i declare this instead of just typedef? Sorry, I'm a noob. Thanks for your help guys!

#include <Windows.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include "ftrScanAPI.h"

typedef FTR_API_PREFIX FTRHANDLE (FTR_API *MyftrScanOpenDevice)();


MyftrScanOpenDevice NowftrScanOpenDevice;


void main()
{
FTRHANDLE hDevice=NULL;
PVOID pBuffer;
FTRSCAN_IMAGE_SIZE ImageSize;

hDevice=LoadLibrary("ftrScanAPI.dll");
if(hDevice==NULL)

{   printf("Failed to detect ScanAPI.dll");
    getch();
    exit(EXIT_FAILURE);
}

hDevice = NowftrScanOpenDevice();
}

Here's a snippet from the API:

FTR_API_PREFIX FTRHANDLE FTR_API ftrScanOpenDevice();

Upvotes: 0

Views: 160

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409356

The variable NowftrScanOpenDevice is a pointer to a function, and as it's a global variable it's zero-initialized. By calling it, you are in effect dereferencing a NULL pointer, which is undefined behavior and leads to the crash.

You need to use GetProcAddress to get the actual address of the function, and assign it to NowftrScanOpenDevice:

...
HMODULE hLibrary = LoadLibrary(...);

NowftrScanOpenDevice = (MyftrScanOpenDevice) GetProcAddress(hLibrary,
                                                            "ftrScanOpenDevice");

hDevice = NowftrScanOpenDevice();
...

Note: I added a new variable hLibrary for the handle returned by LoadLibrary. You need it to later call FreeLibrary.

Upvotes: 1

Related Questions