loading a dll
Dec 9, 2017 at 5:24pm UTC
hi guys I'm trying to load a dll into my C program I got on the web,
but it I seem to be getting an error I checked the file path and it's correct yet it doesn't seem to be working
error I get is C:\Users\User\buffer2\main.c|6|error: initializer element is not constant|
thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
HINSTANCE hDll = LoadLibrary("C:\\Users\\User\\Downloads\\dll.dll" );
int main(int argc,char **argv)
{
char string[16];
strcpy(string,argv[1]);
return 0;
}
Dec 9, 2017 at 5:39pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <windows.h>
int main()
{
const char path[] = "C:/windows/system32/wincorlib.dll" ;
const auto base_address = ::LoadLibraryA(path) ;
std::cout << "library '" << path << "' loaded at address " << base_address << '\n' ;
FreeLibrary(base_address) ;
}
http://rextester.com/MYPW60282
Dec 9, 2017 at 6:06pm UTC
hi JL
not sure what that code is doing?
will this fix my dll error above?
thanks
Dec 9, 2017 at 6:24pm UTC
C does not support dynamic initialisation of variables with static storage duration.
If
hDll must be a variable declared at global scope, something like this is required in C:
1 2 3 4 5 6 7 8 9 10 11
#include <windows.h>
HMODULE hDll = NULL ; // initialise with the constant NULL
int main(int argc,char **argv)
{
// load the library and assign the result
hDll = LoadLibraryA("C:\\Users\\User\\Downloads\\dll.dll" );
// ...
}
Dec 9, 2017 at 7:09pm UTC
Thanks JL
would it be easier just to change it to a cpp file?
thanks
Dec 9, 2017 at 8:07pm UTC
> would it be easier just to change it to a cpp file?
Yes.
Dec 9, 2017 at 8:35pm UTC
thanks JL
I think I will do that =)
Topic archived. No new replies allowed.