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 关于大棚监测的pcb板设计
    • ¥20 sim800c模块 at指令及平台
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计