Skip to content
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

Issue in calling a function from DLL in GO Lang #40890

Closed
Testcase4 opened this issue Aug 19, 2020 · 14 comments
Closed

Issue in calling a function from DLL in GO Lang #40890

Testcase4 opened this issue Aug 19, 2020 · 14 comments

Comments

@Testcase4
Copy link

@Testcase4 Testcase4 commented Aug 19, 2020

Problem Description : There is dll( A1.dll- compiled in C++) in which a function is defined as
EXPORT void S3(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow){}

A1.dll
`// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include
//#include

#define EXPORT extern "C" __declspec(dllexport)

EXPORT void S3(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
MessageBox(NULL, TEXT("Hello World"),
TEXT("In a DLL"), MB_OK);
}
**I want to load this DLL using GO lang. For this I am writing below program:**package main

import (
"fmt"
"syscall"
"golang.org/x/sys/windows"
)

func main() {
dll, err := syscall.LoadDLL("A1.dll")
proc, err := dll.FindProc("S3")
_, _, dllError := proc.Call()
fmt.Println("calling TestPP")
fmt.Printf("Error, err: %s\n", dllError)
fmt.Printf("Error, err: %s\n", err)
}`

This is not working. Also I am not able to understand that here how to pass the parameter in call function according to function S3 definition. Could anybody help me in understanding this issue?

@davecheney
Copy link
Contributor

@davecheney davecheney commented Aug 19, 2020

What did you see on your computer when you ran this program?

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

Setup : Visual Studio Code ( Windows 10)

Debug Console Output

API server listening at: 127.0.0.1:22699
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0xd9c069]

goroutine 1 [running]:
syscall.(*DLL).FindProc(0x0, 0xdf3ff4, 0x6, 0x0, 0x0, 0x0)
C:/Go/bin/src/syscall/dll_windows.go:115 +0x109
main.main()
c:/Users/Dell/Desktop/hello.go:17 +0xf3

@davecheney
Copy link
Contributor

@davecheney davecheney commented Aug 19, 2020

dll, err := syscall.LoadDLL("A1.dll")

Check the error here, dll is nil

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

Hi

Yes, Right dll is nil.
Why it is nil ? Is it some locating issue ?
A1.dll is present on the Desktop and Hello.go is also present in the Desktop.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Aug 19, 2020

Why it is nil ? Is it some locating issue ?

if you check err it will tell you why.

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

It goes into syscall_windows.go
there it enters into the function "syscall_loadlibrary"
there It fails at
if handle == 0 {
err = c.err
}
and failed to load dll.
Still I am unable to understand the reason. Would you please explain it ?
Is there any problem in Dll ?
I have loaded it C++. It is working perfectly.

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Aug 19, 2020

failed to load dll.
Still I am unable to understand the reason. Would you please explain it ?

Is A1.dll file in the same directory as your program?

Alex

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

failed to load dll.
Still I am unable to understand the reason. Would you please explain it ?

Is A1.dll file in the same directory as your program?

Alex

Yes Both are present on the Desktop.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Aug 19, 2020

Please add this line

fmt.Println(err)

After this line

dll, err := syscall.LoadDLL("A1.dll")

And paste the result

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

API server listening at: 127.0.0.1:32073
Failed to load Dll2.dll: %1 is not a valid Win32 application.

Note: I changed the Dll name ( A1 -> Dll2)

I wrote the Dll code using C++ in Visual Studio 2017 and Configuration is as follows :
Active Solution Configuration : Release
Active Solution Platform : x86 (Win32)

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

API server listening at: 127.0.0.1:32073
Failed to load Dll2.dll: %1 is not a valid Win32 application.

Note: I changed the Dll name ( A1 -> Dll2)

I wrote the Dll code using C++ in Visual Studio 2017 and Configuration is as follows :
Active Solution Configuration : Release
Active Solution Platform : x86 (Win32)

API server listening at: 127.0.0.1:32073
Failed to load A1.dll: %1 is not a valid Win32 application.

I wrote the Dll code using C++ in Visual Studio 2017 and Configuration is as follows :
Active Solution Configuration : Release
Active Solution Platform : x86 (Win32)

@davecheney
Copy link
Contributor

@davecheney davecheney commented Aug 19, 2020

Googling this error message suggests that there is a mismatch between the architecture that your dll is compiled with and the version of Go you are using.

Would you please complete the issue template so we can investigate further. Thank you

https://github.com/golang/go/blob/master/.github/ISSUE_TEMPLATE

@Testcase4
Copy link
Author

@Testcase4 Testcase4 commented Aug 19, 2020

Googling this error message suggests that there is a mismatch between the architecture that your dll is compiled with and the version of Go you are using.

Would you please complete the issue template so we can investigate further. Thank you

https://github.com/golang/go/blob/master/.github/ISSUE_TEMPLATE

Hi,
Thanks. It is working. Problem is in loading Dll. I respect your time and response.
I updated go architecture (386) and It is working flawlessly.
One thing I want to ask : Is there not golang for Visual Studio 2017 or 2019 ?
Also , which platform should I use for golang development so that It is easy for me to debug the issue if occurs ?

@davecheney
Copy link
Contributor

@davecheney davecheney commented Aug 19, 2020

Thank you for confirming you've been able to fix the problem.

As to your other questions, unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@davecheney davecheney closed this Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.