dynamic use dll or so file error!

master djj at shumtn.com
Wed Nov 27 06:33:18 PST 2013


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;
}


More information about the Digitalmars-d mailing list