loading a dll

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;
}
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
hi JL

not sure what that code is doing?

will this fix my dll error above?

thanks
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");

    // ...
}
Thanks JL

would it be easier just to change it to a cpp file?

thanks
> would it be easier just to change it to a cpp file?

Yes.
thanks JL

I think I will do that =)
Topic archived. No new replies allowed.