Giter Club home page Giter Club logo

godll's Introduction

goDLL

Project to show people how to create MS Windows golang DLL, then load it into lazarus program, or a plain fpc command line program or another golang exe. This implies you can also use the DLL in plain C or C++ Visual Studio or GCC project. Should also be able to port this to bsd/linux/macOS

Current way of making a DLL:

  • compile go code with regular go compiler and put that puts code into .a and .h files for you. These are just like C code that GCC can use, but stored in compiled .a and .h files
  • then build a dll based on the dummy .c file (CGO) that just links the .a and .h files (go code) into a dll, using CGO (GCC automatically from go) to build the dll

Well known issues with Go DLL's:

  • AFAIK you can only add one go dll to a program, and only once, as multiple go dlls in the same program will confluct each other's go runtime at this time
  • Unloading a dll will crash program... But one go dll loaded, and kept loaded, works..
  • This may improve in the future as golang repairs these issues if they can.
  • A go runtime (which is included in all golang programs/code) is similar to a borland BPL or a .net runtime: multiple runtimes can be tricky/conflicting. It can even overwrite data if multiple runtimes exist, possibly even more tricky than a BPL. This again could be fixed in the future by golang authors if they work on dll's and unloading more in the future.
  • regarding unix dll/so: I have not tested or read about it enough to know the details. This project is just a Microsoft Windows DLL project example, could also work on unix
  • Go DLL, not "God II"

Other ideas:

  • Try statically linking go code into fpc, instead of dll put the code right into the exe directly using .a files, and making an fpc .h equivalent (should be easy)
  • show its use also in a plain c program since not all people interested in go dll's are using fpc/lazarus
  • try come up with solutions to fix "multiple go runtime" problems (multiple go DLLs loaded) and "unloading" (of dll) problems. See golang open issues 11100 and 11058

godll's People

Contributors

z505 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

godll's Issues

Issue in calling a function from DLL in GO Lang

Problem Description : There is dll( A1.dll- compiled in C++) in which a function is defined as
EXPORT void S3(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow){}

A1.dll
`// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include
//#include

#define EXPORT extern "C" __declspec(dllexport)

EXPORT void S3(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
MessageBox(NULL, TEXT("Hello World"),
TEXT("In a DLL"), MB_OK);
}
**I want to load this DLL using GO lang. For this I am writing below program:**package main

import (
"fmt"
"syscall"
"golang.org/x/sys/windows"
)

func main() {
dll, err := syscall.LoadDLL("A1.dll")
proc, err := dll.FindProc("S3")
_, _, dllError := proc.Call()
fmt.Println("calling S3")
fmt.Printf("Error, err: %s\n", dllError)
fmt.Printf("Error, err: %s\n", err)
}`

This is not working. Also I am not able to understand that here how to pass the parameter in call function according to function S3 definition. Could anybody help me in understanding this issue?

Return **C.Char

Been trying to return a string, not printing it to console, but there seem not to be a way.

I did try 2 different approaches

//export PrintHello2
func PrintHello2(Input *C.char, Output **C.char) int32 {
	*Output = C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
	return 1
}

and

//export PrintHello4
func PrintHello4(Input *C.char) *C.char{
	return C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
}

I'm calling this code from .net using the following test code

class Program
{
	[DllImport("goDLL.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
	public static extern int PrintHello2([In] byte[] data, [MarshalAs(UnmanagedType.I1)] ref SByte[] output);

	[DllImport("goDLL.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
	public static extern SByte[] PrintHello4(byte[] data);

	static void Main(string[] args)
	{
		string res = "demo";
		SByte[] output= { };

		Console.WriteLine("PrintHello4 Returns: " + PrintHello4(Encoding.UTF8.GetBytes(res)).Length);

		Console.WriteLine("PrintHello2 Returns: " + PrintHello2(Encoding.UTF8.GetBytes(res), ref output));
		Console.WriteLine("Ref Val changed to: " + output.Length + "\n");
	}
}

None of the Go function works as expected and program chashes with System.Runtime.InteropServices.MarshalDirectiveException for any of the 2 functions

DLL with parameters

When try to export as in godll.c
PrintHello(GoString p1);
goDLL.c: In function 'dummy':
goDLL.c:6:13: error: expected expression before 'GoString'
6 | PrintHello(GoString p1);
| ^~~~~~~~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.