dqwh0108 2019-05-17 05:39
浏览 72

运行C应用程序时出现运行时应用程序错误,该应用程序从使用c-shared创建的DLL中导入方法

Created a simple console application which imports functions exposed in the DLL and the DLL was created using c-shared option in golang. The go DLL depends on other DLL which was created using C++. The dependent DLL are placed in the correct path so console application is able to locate them while loading. "The application was unable to to start correctly(0x0000142)" error is seen while launching console application.

I tried it on Windows10 machine and tried to create executable program in go using cgo to add dependency on other DLL. The executable works fine but when the same is turned into DLL and then issue is while importing the DLL in C application.

Check DLL were created using following code

check.h

#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) char* testing1(char* input);
__declspec(dllexport) char* testing2(char* input);

#ifdef __cplusplus
}
#endif

check.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define DllExport __declspec(dllexport)

extern "C" __declspec(dllexport) char* testing1(char* input){
    return input;
}
extern "C" __declspec(dllexport) char* testing2(char* input){
    return input;
}

int main(int argc, char* argv[])
{
    char* result = testing1("testing ");
    std::cout<<"Result : "<< result<<std::endl;
}```

Go code for creating DLL which dependency on above created Check.DLL
package main

/*
#cgo LDFLAGS: -lCheck
#include <check.h>
#include <stdlib.h>
#include <string.h>
*/
import "C"
import "fmt"

//export AddTwoNums
func AddTwoNums(numone int, numtwo int) int {
    return numone + numtwo
}

//export Validate
func Validate(args *C.char) *C.char {
    str := C.GoString(args)
    str = str + "-" + str
    return C.CString(str)
}

func importFromClib1() {
    str1 := "Testing1"
    cstr := C.CString(str1)
    result := C.testing1(cstr)
    fmt.Println("Testing1 result : ", C.GoString(result))
}

func importFromClib2() {
    str1 := "Testing2"
    cstr := C.CString(str1)
    result := C.testing2(cstr)
    fmt.Println("Testing2 result : ", C.GoString(result))
}

func main() {
} 

Expected the importing of function AddTwoNums and Validate should not cause any issue while running the console application. But the error mentioned in the problem is seen.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 cgictest.cgi文件无法访问
    • ¥20 删除和修改功能无法调用
    • ¥15 kafka topic 所有分副本数修改
    • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
    • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
    • ¥40 串口调试助手打开串口后,keil5的代码就停止了
    • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
    • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
    • ¥50 C++五子棋AI程序编写
    • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。