File: hello_world-load-dll-at-runtime.cpp

package info (click to toggle)
bazel-bootstrap 4.2.3%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,476 kB
  • sloc: java: 721,710; sh: 55,859; cpp: 35,359; python: 12,139; xml: 295; objc: 269; makefile: 113; ansic: 106; ruby: 3
file content (30 lines) | stat: -rwxr-xr-x 706 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <windows.h>
typedef char *(__cdecl *GET_TIME_PTR)();
typedef void(__cdecl *SAY_HELLO_PTR)(char *);

int main() {
  HINSTANCE hellolib;
  GET_TIME_PTR get_time;
  SAY_HELLO_PTR say_hello;

  bool success = FALSE;

  hellolib = LoadLibrary(TEXT("hellolib.dll"));

  if (hellolib != nullptr) {
    get_time = (GET_TIME_PTR)GetProcAddress(hellolib, "get_time");
    say_hello = (SAY_HELLO_PTR)GetProcAddress(hellolib, "say_hello");

    if (nullptr != get_time && nullptr != say_hello) {
      success = TRUE;
      char *now = get_time();
      say_hello(now);
    }
    FreeLibrary(hellolib);
  }

  if (!success) printf("Failed to load dll and call functions\n");

  return 0;
}