Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • First the good news - this solution did allow me to call a member function! Now the bad news - the compiler is warning that std::wstring is not valid in C-linkage functions. Now, warnings aren't errors. But when I run it, calling that function results in a crash. It's the same place Python crashes via ctypes, so at least I reached parity. Now I'm left wondering whether the issue is related to the wstring and C linkage; or the other possibility is that some internal state of the library is wrong, and I'd have to figure that out first. Commented Sep 24 at 5:40
  • @NickBauer: Hm. Are you marking any of these functions as extern "C"? If so, stop doing that. I can confirm that marking f as extern "C" here seems to make it give wrong codegen. So the fix is don't do that, then. Commented Sep 24 at 22:51
  • I'm not, but I'm wondering if its taking up the state of the enclosing function? Commented Sep 25 at 15:32
  • Probably need to post the complete code for your shim DLL, then (directly in your question if it's short, or via a Godbolt link if not). Also: The original DLL is giving you a bunch of bytes it claims is a wstring. What does that bunch of bytes look like in hex? (Define your own struct WString { alignas(8) char bytes_[sizeof(std::wstring)]; ~WString() {} }; and print out the bytes. This could show that (contrary to my expectation) the DLL was built with a different idea of what wstring is than what's in your current platform's Microsoft STL. Commented Sep 25 at 15:33
  • It seems I just needed to move the C++ code into a separate function that returns a C string, and just have a thin extern "C" wrapper around the call to the C++ function. Don't know if it works yet, but compiler stops complaining. Commented Sep 25 at 16:45