cpp 调用dll写法

go语言编译 dll

SET CGO_ENABLED=1

go build  -ldflags "-w -s"  -o searchEngine.dll -buildmode=c-shared shared.go




//export Init
func Init(ppath string) {
	mu.Lock()
	defer mu.Unlock()
	change = true
	loadPath = ppath
}

//export RemoveIndex
func RemoveIndex() {
	mu.Lock()
	defer mu.Unlock()
	index.DestroyAndRemoveIndexFiles()
}

//export Search
func Search(key string, typed int) *C.char {

	lazyLoad()
	mu.RLock()
	defer mu.RUnlock()
	if loadPath == "" {
		return C.CString("@NullLoadPath")
	}
	res := index.Search(key, typed)
	if res == nil {
		return C.CString("@Empty")
	}
	var ok []string
	for _, v := range res {
		ok = append(ok, v.ID)
	}
	bs, _ := json.Marshal(ok)

	return C.CString(string(bs))
}

cpp 调用 dll

#include<iostream>

#include <windows.h>
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package command-line-arguments */


#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.  */




/* 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 __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  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 __declspec(dllexport) void Init(GoString ppath);
extern __declspec(dllexport) void RemoveIndex();
extern __declspec(dllexport) char* Search(GoString key, GoInt typed);

#ifdef __cplusplus
}
#endif


//struct SearchEngine {
//
//    void debug() {
//
//    }
//}
HINSTANCE dllHandle = NULL;
bool isLoaded = false;
HINSTANCE init_dll() {
    if(dllHandle) {
        return dllHandle;
    }

    dllHandle = LoadLibrary("searchEngine.dll");
    return dllHandle;
}
void removeIndex() {

    auto handle = init_dll();
    //RemoveIndex
    typedef void (*InitFunc)();
    auto func = (InitFunc)GetProcAddress(handle, "RemoveIndex");
    if(func) {
         func();
         printf("\nfree index\n");
    }
    
    
}
char* search(const char* p) {
    typedef char* (*Func)(GoString,int);
    auto handle = init_dll();
    if(!isLoaded) {
        typedef void (*InitFunc)(GoString);
        auto func = (InitFunc)GetProcAddress(handle, "Init");
        if(!func) {
            printf("error no func\n");
            return NULL;
        }
        GoString s;
        s.p = "D:/ASUS/Desktop/plug/quickcodesearch";
        s.n = strlen(s.p);
        func(s);
        printf("in-> %s\n",s.p);
        isLoaded = true;
    }
    Func ffunc = (Func)GetProcAddress(handle,"Search");
    GoString buf;
    buf.p = p;
    buf.n = (unsigned long long)strlen(p);
    return ffunc(buf,0);
    // return res
    // return buf;
}
//bool loadDll = false;
//HINSTANCE init_dll() {
//    if(loadDll) {
//        return dllHandle;
//    }
//    loadDll = true;
//    dllHandle = LoadLibrary("searchEngine.dll");
//    return dllHandle;
//
//}
//
//
//void _dllEngineSetIndexPath(const char* in) {
//    HINSTANCE handle = init_dll();
//
//    if (handle != NULL) {
//         typedef void (*Func)(GoString );
//         Func ffunc = (Func)GetProcAddress(handle,"SetIndexPath");
//         if(ffunc) {
//            GoString buf;
//            buf.p = in;
//            buf.n = (unsigned long long)strlen(in);
//            ffunc(buf);
//            return;
//         }
//    }
//}
//void _dllSearch(const char* in) {
//    HINSTANCE handle = init_dll();
//    printf("dllcall %d\n",handle);
//
//    if (handle != NULL) {
//
//        typedef GoSlice (*Func)(GoString ,int);
//        Func ffunc = (Func)GetProcAddress(handle,"Search");
//
//        if(ffunc) {
//            GoString buf;
//            buf.p = in;
//            buf.n = (unsigned long long)strlen(in);
//            printf("dllcall %d\n",ffunc);
//            GoSlice x = ffunc(buf,0);
//            // for (int i=0;i<x.len;i++) {
//            //     printf("%s",((*GoString)x.data[i]) -> p);
//            // }
//
//        }else {
//            printf("no engine");
//        }
//    }else {
//        printf("error\n");
//    }
//}
//void _dllEngineLoad(const char* in) {
//    HINSTANCE handle = init_dll();
//        printf("dllcall %d\n",handle);
//
//    if (handle != NULL) {
//
//        typedef void (*Func)(GoString);
//        Func ffunc = (Func)GetProcAddress(handle,"LoadDirFiles");
//
//        if(ffunc) {
//            GoString buf;
//            buf.p = in;
//            buf.n = (unsigned long long)strlen(in);
//            printf("dllcall %d\n",ffunc);
//            ffunc(buf);
//
//        }else {
//            printf("no engine");
//        }
//    }else {
//        printf("error\n");
//    }
//
//}

int main(void) {
//    Debug();
    // HINSTANCE handle = init_dll();
    char* p = search("go");
      
    printf("debug [%s]",p);

    free(p);
    removeIndex();
    

     FreeLibrary(dllHandle);

//    cout << "helloworld" <<endl;
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值