I don't know much about C/C++
but I have some knowledge about golang
.
I was playing around with simple lib written in golang
which I would like to use it in simple C++
program. I was thinking it would be possible after I generate .dll
and .h
files.
Here is my golang code
package main
import "C"
//export getHelloWord
func getHelloWord(word string) string {
return word
}
func main() {
}
And I run the following command to generate .dll
and .h
files after installing TDM-GCC-64
go build -o cgo/lib/lib.dll -buildmode=c-shared cgo/lib.go
And here is my C++ file which loads .dll
file generated by above steps and call the getHelloWorld
function.
#include <windows.h>
#include <winbase.h>
#include <iostream>
using namespace std;
typedef string(__stdcall* f_getHelloWord)(string name);
int main()
{
std::cout << "Starting program.\n";
//Loading dll
HINSTANCE hModule = LoadLibrary(TEXT("lib/lib.dll"));
if (!hModule) {
std::cout << "could not load the dynamic library" << std::endl;
return EXIT_FAILURE;
}
// resolve function address here
f_getHelloWord fn = (f_getHelloWord)GetProcAddress(hModule, "getHelloWord");
if (!fn) {
std::cout << "could not locate the function" << std::endl;
return EXIT_FAILURE;
}
std::cout << "getHelloWord() returned " << fn("Hello World") << std::endl;
return EXIT_SUCCESS;
}
When I compile and run the above program. It does not print returned word Hello World
Complete output is
Starting program.
getHelloWord() returned
C:\Users\TEMBO MTOTO\source\repos\cgo\x64\Debug\cgo.exe (process 27048) exited with code 0.
Press any key to close this window . . .
But when change go function to receive and return int
instead of string it really works as expected. See code below..
package main
import "C"
//export getHelloWord
func getHelloWord(number int) int {
return 7888 * number
}
func main() {
}
When I run program I get the following errors
#include <windows.h>
#include <winbase.h>
#include <iostream>
using namespace std;
typedef int(__stdcall* f_getHelloWord)(int n);
int main()
{
std::cout << "Starting program.\n";
//Loading dll
HINSTANCE hModule = LoadLibrary(TEXT("lib/lib.dll"));
if (!hModule) {
std::cout << "could not load the dynamic library" << std::endl;
return EXIT_FAILURE;
}
// resolve function address here
f_getHelloWord fn = (f_getHelloWord)GetProcAddress(hModule, "getHelloWord");
if (!fn) {
std::cout << "could not locate the function" << std::endl;
return EXIT_FAILURE;
}
std::cout << "getHelloWord() returned " << fn(10000) << std::endl;
return EXIT_SUCCESS;
}
complete output is
Starting program.
getHelloWord() returned 78880000
So why string doesn't work? Any help will be much appreciated..
Code repository is available on GitHub
p
is the data andn
is a length. – Bluenosestd::string golang = "Golang"; GoString input; input.p = golang.c_str(); input.n = golang.size();
– Bluenose__SIZE_TYPE__
tolong long
. Probably it helps. But later you can have problems with linking. – Bluenose.dll
file which now I am trying to use it in C++. I have update my question with new code. Could you take a look – Intellectextern __declspec(dllexport) GoString getHelloWord(GoString name);
not likeextern __declspec(dllexport) GoString getHelloWord(std::string name);
. GoString and std::string it's a different types. You should use GoString – Bluenose.dll
alone – Intellect