windows go dll 框架

乘着还没有添加商业功能之前,先给大家把福利分享了

希望有需要的朋友能够用的上

这个框架是在用windows平台,GO做的http/https服务,调用dll现有的库接口实现特定功能的大框架

//dll

//现有库不能直接使用,必须要有一个代理库,这个必要性,我想大家都清楚

//XSKFCSPEnroll是我司商业库,这个就不能让诸位浏览了,大家都能理解

//Enroll库是代理库

//我们以获得调用的商业库版本为例来阐述

//Enroll.h

#ifndef _ENROLL_
#define _ENROLL_

# ifdef ENROLL
# define Export _declspec(dllexport)
# else
# define Export _declspec(dllimport)
# endif

extern "C" {

Export int getHandle(void** pEnrollHandle, void** pLibHandle);
Export int releaseHandle(void** pEnrollHandle, void** pLibHandle);

Export int freeMem(void** pEnrollHandle, void** pLibHandle, void** p);

Export int getVersion(void** pEnrollHandle, void** pLibHandle, char** ver);

}

#endif

//Enroll.cpp

#include "stdafx.h"
#define ENROLL

#include "Enroll.h"
#include "app_util.h"

Export int getHandle(void** pEnrollHandle, void** pLibHandle)
{
  HMODULE handle = LoadLibrary("XSKFCSPEnroll.dll");
  if (handle)
  {
    typedef void*(*F)();
    F f = (F)GetProcAddress(handle, "getLibraryHandle");
    if (!f)
    {
      FreeLibrary(handle);
    }
    else
    {
      void* libhandle = f();
      if (!libhandle)
      {
        FreeLibrary(handle);
      }
      else
      {
        *pEnrollHandle = handle;
        *pLibHandle = libhandle;
        return 1;
      }
    }
  }

  return 0;
}

Export int releaseHandle(void** pEnrollHandle, void** pLibHandle)
{
  if (pEnrollHandle && pLibHandle)
  {
    typedef void(*F)(void*);
    F f = (F)GetProcAddress((HMODULE)*pEnrollHandle, "releaseLibraryHandle");
    if (!f)
    {
      FreeLibrary((HMODULE)*pEnrollHandle);
    }
    else
    {
      f(*pLibHandle);
      FreeLibrary((HMODULE)*pEnrollHandle);
      return 1;
    }
  }

  return 0;
}

Export int freeMem(void** pEnrollHandle, void** pLibHandle, void** p)
{
  if (p&&(*p))
  {
    free(*p);
    return 1;
  }

  return 0;
}

Export int getVersion(void** pEnrollHandle, void** pLibHandle, char** ver)
{
  if (pEnrollHandle && pLibHandle)
  {
    typedef HRESULT (*F)(void*, BSTR*);
    F f = (F)GetProcAddress((HMODULE)*pEnrollHandle, "get_Version");
    if (f)
    {
      CComBSTR ret;
      if (S_OK==f(*pLibHandle, &ret))
      {
        CMemBlock<char> c_ret = CUtf8Unicode::f8UStringConvert(ret);
        *ver = (char*)malloc(c_ret.GetSize());
        if (*ver)
        {
          memcpy(*ver, c_ret, c_ret.GetSize());
          return 1;
        }
      }
    }
  }

  return 0;
}

//Enroll_Server.go

// 支持http/https

//遗留问题:服务如何退出,现在我还不知道

package main

import (
  "fmt"
  "os"
  "net/http"
  "Enroll_lib"
  //"encoding/json"
)

var w32_handle Enroll_lib.Win32_handle

func init() {
  fmt.Println("init")
  var pw32 *Enroll_lib.Win32_handle = &w32_handle
  res := pw32.LoadLib()
  if !res {
    fmt.Println("Load library failure")
  }
  res = pw32.GetHandle()
  if !res {
    fmt.Println("GetHandle failure")
  }
  //demo
  fmt.Println("Version: ", pw32.GetVersion())
}

func entry(res http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(res, "Hello World!\n")
}

func main() {
  Port := "8086"
  IsHttp := true
  arg_num := len(os.Args)
  if 2<=arg_num {
    Port = os.Args[1]
  }
  if 3<=arg_num {
    if os.Args[2]=="true" {
      IsHttp = true
    } else {
      IsHttp = false
    }
  }
  fmt.Printf("server is http %t\n", IsHttp)
  fmt.Println("server listens at ", Port)

  http.HandleFunc("/", entry)

  var err error
  if IsHttp {
    err = http.ListenAndServe(":"+Port, nil)
  } else {
    err = http.ListenAndServeTLS(":"+Port, "server.crt", "server.key", nil)
  }
  if err != nil {
    fmt.Println("Server failure /// ", err)
  }

  w32_handle.FreeLib();
  fmt.Println("quit");
}

//libs

//自开发的Go库

package Enroll_lib

import(
  "fmt"
  "syscall"
  "C"
  "unsafe"
)

type Win32_handle struct {
  handle syscall.Handle
  c_enroll_handle uintptr
  c_lib_handle uintptr
  err error
}

func (h *Win32_handle) LoadLib() bool {
  h.handle, h.err = syscall.LoadLibrary("Enroll.dll")
  if h.err!=nil {
    fmt.Println("Enroll.dll not found")
    return false
  }

  return true
}

func (h *Win32_handle) FreeLib() {
  syscall.FreeLibrary(h.handle)
}

func (h *Win32_handle) GetHandle() bool{
  getHandle, err := syscall.GetProcAddress(h.handle, "getHandle")
  if err!=nil {
    fmt.Println("getHandle not found")
    return false
  }
  r,_,retstr := syscall.Syscall(uintptr(getHandle), 3, uintptr(unsafe.Pointer(&h.c_enroll_handle)),
                uintptr(unsafe.Pointer(&h.c_lib_handle)),
                0)
  fmt.Println(retstr);
  if r==0 {
    fmt.Println("syscall failuer at getHandle")
    return false
  }

  return true
}

func (h *Win32_handle) ReleaseHandle() {
  releaseHandle, err := syscall.GetProcAddress(h.handle, "ReleaseHandle")
  if err!=nil {
    fmt.Println("releaseHandle not found")
  }
  r,_,retstr := syscall.Syscall(uintptr(releaseHandle), 3, uintptr(unsafe.Pointer(&h.c_enroll_handle)),
                uintptr(unsafe.Pointer(&h.c_lib_handle)),
                   0)
  fmt.Println(retstr)
  if r==0 {
    fmt.Println("syscall failuer at releaseHandle")
  }
}

func (h *Win32_handle) FreeMem(p *C.char) {
  freeMem, err := syscall.GetProcAddress(h.handle, "freeMem")
  if err!=nil {
    fmt.Println("freeMem not found")
  }
  r,_,retstr := syscall.Syscall(uintptr(freeMem), 3, uintptr(unsafe.Pointer(&h.c_enroll_handle)),
                uintptr(unsafe.Pointer(&h.c_lib_handle)),
                uintptr(unsafe.Pointer(&p)))
  fmt.Println(retstr)
  if r==0 {
    fmt.Println("syscall failuer at freeMem")
  }
}

func (h *Win32_handle) GetVersion() string {
  getVersion, err := syscall.GetProcAddress(h.handle, "getVersion")
  if err!=nil {
    fmt.Println("getVersionm not found")
  }
  var p *C.char
  r,_,retstr := syscall.Syscall(uintptr(getVersion), 3, uintptr(unsafe.Pointer(&h.c_enroll_handle)),
                uintptr(unsafe.Pointer(&h.c_lib_handle)),
                uintptr(unsafe.Pointer(&p)))
  fmt.Println(retstr)
  if r==0 {
    fmt.Println("syscall failure at getVersion")
  }

  defer h.FreeMem(p)

  return C.GoString(p)
}

//现场

windows go dll 框架

//结果-https

windows go dll 框架

Finally:

大家是不是迫不及待的想GO了,如果是,那我很欣慰!

本系列Go讲解暂时得告一段落啦

其实,我真心希望,Go&C能够一统江湖,让其它那些乱七八糟的语言自然衰老去dead

哈哈哈哈哈,上边这句你就当笑话听吧!权当我没说

不听才怪!哈哈哈

劝告:

1. 永远不要说自己的结论和推论的正确性

2. 伯乐只有一个,可惜不是你

3. 客观,唯物,在同事实践中,你才能知道一个人到底行不行

最后,祝大家Go(滚)的顺溜!哈哈哈

上一篇:你(可能)不知道的web api


下一篇:基础概念:Oracle数据库