作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将一个 c-dll 注入(inject)一个进程。在 c-dll 中,它将加载另一个由 golang 编写的 dll。这是我的 C 代码:
当loader.dll被加载时,它会自动加载用golang编写的worker.dll。
// loader.c
#include <windows.h>
#include <stdio.h>
#include <memory.h>
typedef void (*StartWorker)();
HMODULE hWorker = NULL;
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
hWorker = LoadLibrary("D:\\code\\toys\\worker.dll");
if (hWorker == NULL)
{
exit(1);
}
StartWorker startWorker = (StartWorker)GetProcAddress(hWorker, "StartWorker");
MessageBox(NULL, "worker starting", TEXT("Warning:"), MB_OK);
if (startWorker == NULL)
{
MessageBox(NULL, "error", TEXT("Warning:"), MB_OK);
exit(1);
}
startWorker();
MessageBox(NULL, "worker started", TEXT("Warning:"), MB_OK);
FreeLibrary(hWorker);
break;
}
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
default:
break;
}
return TRUE;
}
去代码是:
// worker.go
package main
/*
#include <stdlib.h>
#include <windows.h>
*/
import "C"
//export StartWorker
func StartWorker {
C.MessageBox(nil, C.CString("Hello Worker"), C.CString("Warning:"), 0)
}
func main() {
}
我已将它们编译到
MinGW-w64
.当 loader.dll 试图调用
StartWorker()
在worker.dll 中,进程没有显示MessageBox。在我用c重写worker.dll后,一切正常。我也可以调用
StartWorker()
通过以下代码:
#include <windows.h>
#include <stdio.h>
#include <memory.h>
typedef void (*StartWorker)();
int main()
{
char *dllPath = "D:\\code\\toys\\loader.dll";
HMODULE hWorker = NULL;
hWorker = LoadLibrary(dllPath);
StartWorker startWorker = (StartWorker)GetProcAddress(hWorker, "StartWorker");
if (startWorker == NULL)
{
MessageBox(NULL, "dllPath", TEXT("Warning:"), MB_OK);
exit(1);
}
startWorker();
MessageBox(NULL, "target", TEXT("Warning:"), MB_OK);
FreeLibrary(hWorker);
return 0;
}
我想知道是否与 go-runtime 有任何冲突?
最佳答案
测试很好,也许你调用了 Microsoft-Windows-Only
api。尝试将 MessageBox()
(它使用 win32 user32.dll
)更改为 Sum()
以测试称为 c-dll
的 go-dll
。
这是转到 gen go-dll
的文件
//gosum.go
package main
/*
#include <stdlib.h>
*/
import "C"
//export Sum
func Sum(a int32, b int32) int32 {
return a + b
}
func main() {
}
去构建 c-shared
Windows
dll:
go build -ldflags "-s -w" -buildmode=c-shared -o gosum.dll
您可以在 c/cpp 中编写测试文件:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "gosum.h"
int main() {
printf("Sum(1,2)=%d\n", Sum(1,2));
return 1;
}
在 MinGW-w64 中编译
g++ -c test_gosum.cpp -o test_gosum.o -g -std=c++11
g++ test_gosum.o gosum.dll -o test_gosum.exe -g -std=c++11
.\test_gosum.exe
loader.dll
中构建
Visual Studio
称为
gosum.dll
//复制
go-dll
CGO gen
gosum.h
文件到项目,可能你需要删除一些错误代码
/* Code generated by cmd/cgo; DO NOT EDIT. */
/* package gosum */
#line 1 "cgo-builtin-export-prolog"
#include <stddef.h> /* for ptrdiff_t below */
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char* p; ptrdiff_t n; } _GoString_;
#endif
#endif
/* Start of preamble from import "C" comments. */
#line 3 "gosum.go"
#include <stdlib.h>
#line 1 "cgo-generated-wrapper"
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef float GoFloat32;
typedef double GoFloat64;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*) == 64 / 8 ? 1 : -1];
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void* GoMap;
typedef void* GoChan;
typedef struct { void* t; void* v; } GoInterface;
typedef struct { void* data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern GoInt32 Sum(GoInt32 a, GoInt32 b);
#ifdef __cplusplus
}
#endif
//pch.h 文件(由 vs 生成,需要添加一些东西):
#define PCH_H
#include "framework.h"
#endif
// ----- added
#ifdef IMPORT_DLL
#else
#define IMPORT_DLL extern "C" _declspec(dllimport)
#endif
IMPORT_DLL int get_sum_by_another_dll(int a, int b);
// ----- added
//dllmain.cpp 文件(由 vs 生成):
#include "pch.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//loader.cpp 文件:
#include "pch.h"
#include <stdlib.h>
#include <windows.h>
#include "gosum.h" // also need add `gosum.dll` to VS project file
using namespace std;
typedef int (CALLBACK* GOSUMFUNC)(GoInt32 a, GoInt32 b);
int __stdcall get_sum_by_another_dll(int a, int b)
{
HMODULE h = LoadLibrary(TEXT("gosum.dll"));
if (NULL == h || INVALID_HANDLE_VALUE == h) {
return 0;
}
GOSUMFUNC go_dll_sum = (GOSUMFUNC)GetProcAddress(h, "Sum");
if (go_dll_sum) {
return go_dll_sum(a, b);
}
return 1;
}
然后在 VS 中编译它们,你会得到
loader.dll
。
gosum.dll
是
go-dll
,
loader.dll
是由 VisualStudio 编译的
c-dll
,称为
gosum.dll
。
gosum.dll
和
loader.dll
复制到同一个目录下。
package main
import (
"fmt"
"log"
"syscall"
)
func main() {
h, e := syscall.LoadLibrary("loader.dll") //Make sure this DLL follows Golang machine bit architecture (64-bit in my case)
if e != nil {
log.Fatal(e)
}
defer syscall.FreeLibrary(h)
proc, e := syscall.GetProcAddress(h, "get_sum_by_another_dll") //One of the functions in the DLL
if e != nil {
log.Fatal(e)
}
var a int32 = 2
var b int32 = 5
ret, _, err := syscall.Syscall(proc, 2, uintptr(a), uintptr(b), 0) //Pay attention to the positioning of the parameter
fmt.Println(ret)
fmt.Println(err)
}
运行它你会得到结果:
get_sum_by_another_dll(2,5) = 7
gosum.dll
和
loader.dll
复制到同一个目录下。
#include <iostream>
#include <Windows.h>
int main()
{
HINSTANCE hDllInst;
hDllInst = LoadLibrary(L"loader.dll");
typedef int(*SUMFUNC)(int a, int b);
SUMFUNC dll_sum_fun = (SUMFUNC)GetProcAddress(hDllInst, "get_sum_by_another_dll");
std::cout << dll_sum_fun(1, 2);
}
然后在VS中编译,运行./callloader.exe,你会得到结果:
get_sum_by_another_dll(1,2) = 3
关于c - 是否可以在 Windows 上的 c-dll 中加载 go-dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65304131/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!