-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
c-shared (DLL) library created for Windows fails to initialize runtime when called from C# application #26714
Comments
Why You Go code says
Your C# code must match parameters and return value
Where in your C# code, it says that I also tried building your C# program, and build fails.
What am I doing wrong? Alex |
I think his C# program snippet is incomplete. At a minimum you need to have
at the top in order to make that work. I'm not really sure that C#'s P/Invoke is going to work with a mingw-created DLL. At least, not without some fiddling. In theory the runtime should just be initialized, but the current DLL is built with the expectation that mingw's C runtime will be doing the initialization, not MSVC's C runtime. I don't know how different they are, or what is involved in making it work. As noted below, it might just be a case of giving gcc/mingw the right commands. See also: |
My C# executable is 32bit, so 386 should be the right value there.
It seems right to me, but maybe I'm interpretting this incorrectly?
You're correct. My editor window was scrolled down slightly and I forgot the
Thanks. I'll check out those links and see what I can learn from them. I've seen that some developers have had luck prior to Go 1.10 by building a static library, then wrapping that in a DLL that was built without Go. I've not had luck with that approach yet, but I haven't spent all that much time there yet. https://github.com/z505/goDLL I'd love to get an understanding of what's expected to work with Go's 1.10 support of DLLs. |
Now it all works for me:
Alex |
That's interesting, @alexbrainman. If I'm understanding this correctly, this means that the worries about MSVC vs GCC/mingw compilation for this case shouldn't be an issue. The big differences I'm seeing between what you're doing and what I'm doing are:
For our purposes that might be fine. I would have expected cross-compilation to work though. Is there any chance that you could send me the compiled DLL for this case? I'd love to poke around and see where/how it deviates from the one I've built. Any idea why the cross-compilation would have resulted in the issue I saw? |
Oh, I suppose another difference between your setup and mine is your go version. I'm using the stable build and you're on devel. Is it expected to work as you're showing on stable? |
I do not know what you are referring to.
I use gcc from mingw package that I installed on my WIndows.
I am not. My mingw is installed on my Windows, and I run built program on my Windows.
Sure. How do you want to do that?
The other thing that different is your dll is 32-bit. My both exe and dll are 64-bit. If your exe and dll GOARCH do not match, it would not work. Mind you, I do not use C#, so I would not know how it works and what is required. Alex |
I'm referring to
I tried to mimic what you're doing yesterday and installed mingw on Windows, but I was hitting different problems. I'm now getting
What's interesting is that if I compile the C# file with
That shouldn't be necessary anymore now that I have a version of it that works. |
OK
Googling for 0x8007000B, first hit, I get https://stackoverflow.com/questions/18007967/net-framework-error-hresult-0x8007000b Perhaps it is what is happening to you. You DLL is 386, while your executable amd64. This is not allowed! Alex |
As it turns out, you're absolutely right (again). I had it set to "Any CPU" in Visual Studios, with the assumption that this would select x64 since i'm on an x64 system. I was wrong. When I manually selected x64, it began working as expected. So now the only change is using gcc/mingw on Windows itself as opposed to having used it from a Mac and doing some cross-compilation. At this point I've got something that I think is workable, but I'll keep poking at this to see if I can determine what's causing the differences between the native build and the cross compiled build. |
Sounds like things are basically working, so closing. Please comment if you disagree. |
What version of Go are you using (
go version
)?1.10 darwin/amd64
Also tried 1.10.2, 1.10.3, 1.11beta2
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
I have a very very simple main.go file:
I'm cross-compiling it as a DLL for Windows/x86 from my AMD64 MacOS 10.13 device via:
Then I'm attempting to make using of this DLL from within a C# application compiled with Visual Studios 2017 on Windows 10. Complete source for this application is:
What did you expect to see?
I would expect to see console output that looks like:
What did you see instead?
The process is then hung. If I pause the process in the debugger I see the following:
Looking at the dissassembled version of the DLL, it seems that it's stuck in
_cgo_wait_runtime_init_done
's while true loop checking whether the runtime is up.When I compile this same go file as a unix .so file and link it with a C program on the Mac everything works exactly as expected.
Is this expected to work? It was my understanding that as of Go 1.10 this was supposed to work.
The text was updated successfully, but these errors were encountered: