fork download
  1.  
  2. #include <Windows.h>
  3.  
  4. #include <cstdio>
  5. #include <memory> // for unique_ptr.
  6. #include <type_traits> // for remove_pointer.
  7.  
  8. int main()
  9. {
  10. auto hm1 = ::LoadLibraryW(LR"unko(C:\Projects\TestDll\Debug\TestDll.dll)unko");
  11. if ( hm1 == nullptr )
  12. {
  13. std::printf("load1 failed\n");
  14. return ~0;
  15. }
  16. std::unique_ptr<std::remove_pointer<decltype(hm1)>::type, void (*)(decltype(hm1))>
  17. disposer1(hm1, [](decltype(hm1) h){ ::FreeLibrary(h); });
  18. std::printf("dll1 okay\n");
  19.  
  20. auto hm2 = ::LoadLibraryW(LR"unko(C:\Projects\TestDll\TestDll.dll)unko");
  21. if ( hm2 == nullptr )
  22. {
  23. std::printf("load2 failed\n");
  24. return ~0;
  25. }
  26.  
  27. std::unique_ptr<std::remove_pointer<decltype(hm2)>::type, void (*)(decltype(hm2))>
  28. disposer2(hm2, [](decltype(hm2) h){ ::FreeLibrary(h); });
  29. std::printf("dll2 okay\n");
  30.  
  31. auto *pf1 = reinterpret_cast<long long (__stdcall *)()>(::GetProcAddress(hm1, "f"));
  32. if ( pf1 == nullptr )
  33. {
  34. std::printf("getprocaddress1 failed\n");
  35. return ~0;
  36. }
  37.  
  38. auto *pf2 = reinterpret_cast<long long (__stdcall *)()>(::GetProcAddress(hm2, "f"));
  39. if ( pf2 == nullptr )
  40. {
  41. std::printf("getprocaddress2 failed\n");
  42. return ~0;
  43. }
  44.  
  45. std::printf("%lld\n", pf1()); // 1
  46. std::printf("%lld\n", pf1()); // 2
  47. std::printf("%lld\n", pf1()); // 3
  48.  
  49. std::printf("%lld\n", pf2());; // 1
  50. std::printf("%lld\n", pf2());; // 2
  51. std::printf("%lld\n", pf2());; // 3
  52. }
  53.  
  54. /*
  55.  TestDll.dll
  56.  
  57. #include <Windows.h>
  58.  
  59. namespace unko
  60. {
  61. long long g_count;
  62. }
  63.  
  64. LONGLONG __stdcall f()
  65. {
  66. return ++unko::g_count;
  67. }
  68.  
  69.  
  70. */
  71.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty