www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - dynamic use dll or so file error!

reply "master" <djj shumtn.com> writes:
I want to access dynamic libraries on different platforms, but I 
still can not get the function, so do not write it? Where is 
wrong?

import core.runtime;
import std.stdio;
import std.string;

version(Windows)
{
	import std.c.windows.windows;
}
else version(Posix)
{
	import core.stdc.stdio;
	import core.stdc.stdlib;
	import core.sys.posix.dlfcn;
}

void* getSymbolAddress(void* handle, string name)
{
	version(Windows)
     {
         FARPROC proc = GetProcAddress(cast(HMODULE)handle, 
name.ptr);
         if (proc is null)
         {
			writefln(": can't resolve symbol "~name);
			return null;
         }
         return cast(void*)proc;
     }
	else version(Posix)
     {
         auto cstr = toStringz(symbol);
         return dlsym(handle, cstr);
     }
}

struct Binder(T)
{
	void opCall(string name, void* lib)
	{
		*fptr = getSymbolAddress(lib, name);
		writefln("111111111111111111111"~name);
	}
	private void** fptr;
}

template bindFunc(T)
{
	Binder!(T) bindFunc(inout T a)
	{
		Binder!(T) res;
		res.fptr = cast(void**)&a;
		return res;
	}
}

extern (C)
{
	void function() net_tcp_send;	
}

void LoadPlatform(void* lib)
{
	bindFunc(net_tcp_send)("net_tcp_send", lib);
}

int main()
{
	void* hp  = Runtime.loadLibrary("ShuNet.dll".dup);
     if(hp is null)
     {
         writefln("error loading ShuNet.dll");
         return 1;
     }
	writefln("ShuNet.dll is loaded");

	LoadPlatform(hp);
		
	printf("net_tcp_send=>%p", net_tcp_send);

	net_tcp_send(); // this error

	if(!Runtime.unloadLibrary(hp))
     {
		writefln("error freeing ShuNet.dll");
         return 1;
     }
     writefln("End...");

     return 0;
}
Nov 27 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Wednesday, 27 November 2013 at 14:33:21 UTC, master wrote:
 I want to access dynamic libraries on different platforms, but 
 I still can not get the function, so do not write it? Where is 
 wrong?
runtime.loadlibrary is not yet finished. use dlopen/LoadLibrary instead(forgot about loading D dll's though, its not going to work this way properly(i mean GC/Runtime attaching))
Nov 27 2013
next sibling parent "master" <djj shumtn.com> writes:
On Wednesday, 27 November 2013 at 15:09:45 UTC, evilrat wrote:
 On Wednesday, 27 November 2013 at 14:33:21 UTC, master wrote:
 I want to access dynamic libraries on different platforms, but 
 I still can not get the function, so do not write it? Where is 
 wrong?
runtime.loadlibrary is not yet finished. use dlopen/LoadLibrary instead(forgot about loading D dll's though, its not going to work this way properly(i mean GC/Runtime attaching))
Unfinished? Amount. . . I am writing with reference to forward and, in theory, is finished. Call Walter Bright old man!
Nov 28 2013
prev sibling parent reply "master" <djj shumtn.com> writes:
On Wednesday, 27 November 2013 at 15:09:45 UTC, evilrat wrote:
 On Wednesday, 27 November 2013 at 14:33:21 UTC, master wrote:
 I want to access dynamic libraries on different platforms, but 
 I still can not get the function, so do not write it? Where is 
 wrong?
runtime.loadlibrary is not yet finished. use dlopen/LoadLibrary instead(forgot about loading D dll's though, its not going to work this way properly(i mean GC/Runtime attaching))
Unfinished? Amount. . . I am writing with reference to the http://dlang.org/dll-linux.html and http://dlang.org/dll.html, in theory, is finished. Calls Walter Bright old man! help me!
Nov 28 2013
parent "master" <djj shumtn.com> writes:
On Thursday, 28 November 2013 at 11:33:05 UTC, master wrote:
 On Wednesday, 27 November 2013 at 15:09:45 UTC, evilrat wrote:
 On Wednesday, 27 November 2013 at 14:33:21 UTC, master wrote:
 I want to access dynamic libraries on different platforms, 
 but I still can not get the function, so do not write it? 
 Where is wrong?
runtime.loadlibrary is not yet finished. use dlopen/LoadLibrary instead(forgot about loading D dll's though, its not going to work this way properly(i mean GC/Runtime attaching))
Unfinished? Amount. . . I am writing with reference to the http://dlang.org/dll-linux.html and http://dlang.org/dll.html, in theory, is finished. Calls Walter Bright old man! help me!
haha! Binder!(T) bindFunc(inout T a) is inout error!
Nov 28 2013