www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - export extern (C) void Fun Error

reply =?UTF-8?B?IuaLlueLl+aVo+atpSI=?= <djj shumtn.com> writes:
export c callback fun:

alias void function(int id) ConnectedCallBack;
alias void function(int id, void* data, int len) ReadCallBack;

export extern (C) void AddListenersss( ConnectedCallBack 
connectedCallBack=null, ReadCallBack readCallBack=null )
{
     int id = 0;
     int len = 0;
     version(Windows)
     {
         while(true)
         {
             Thread.sleep( dur!("seconds")( 2 ) );
             id++;
             connectedCallBack(id);

             len+=id;
             Thread.sleep( dur!("seconds")( 1 ) );
             readCallBack(id, null, len);
         }
     }
     else
     {
         //server = new epoll();
     }
}


In addition to the D outside the


[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void OnConnectedCallBack(int id);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void OnReceivedCallBack(int sock, IntPtr pData, 
int len);

[DllImport("ShuNetTcp.dll",EntryPoint = "AddListenersss", CharSet 
= CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void AddListenersss(OnConnectedCallBack 
connectedCallback, OnReceivedCallBack readCallBack );

using:
m_onConnected = new NetServer.OnConnectedCallBack(OnConnected);
m_onReceived = new NetServer.OnReceivedCallBack(OnRead);
NetServer.AddListenersss( m_onConnected, m_onReceived );

call fun:
private void OnConnected(int id)
{
     Console.WriteLine ("连接完成 Id=>" + id.ToString());
}

private void OnRead(int id, IntPtr pdata, int len)
{
}

Received id and len are wrong! :(
Gurus to help me!!!!!!!!!!!!
Apr 26 2012
next sibling parent =?UTF-8?B?IuaLlueLl+aVo+atpSI=?= <djj shumtn.com> writes:
O God, no one answered...
Apr 26 2012
prev sibling parent reply Trass3r <un known.com> writes:
 export c callback fun:

 alias void function(int id) ConnectedCallBack;
 alias void function(int id, void* data, int len) ReadCallBack;
add extern(C) to be safe
Apr 26 2012
parent reply =?UTF-8?B?IuaLlueLl+aVo+atpSI=?= <djj shumtn.com> writes:
On Friday, 27 April 2012 at 01:21:56 UTC, Trass3r wrote:
 export c callback fun:

 alias void function(int id) ConnectedCallBack;
 alias void function(int id, void* data, int len) ReadCallBack;
add extern(C) to be safe
Thank, Trass3r! Finally correct, why have to export these two?
Apr 27 2012
parent reply "Matt Peterson" <ricochet1k gmail.com> writes:
On Friday, 27 April 2012 at 13:02:56 UTC, 拖狗散步 wrote:
 On Friday, 27 April 2012 at 01:21:56 UTC, Trass3r wrote:
 export c callback fun:

 alias void function(int id) ConnectedCallBack;
 alias void function(int id, void* data, int len) ReadCallBack;
add extern(C) to be safe
Thank, Trass3r! Finally correct, why have to export these two?
You specified the C calling convention with UnmanagedFunctionPointer(CallingConvention.Cdecl), and extern(C) means use the C calling convention. Otherwise they'll be expecting a function using the D calling convention, which is incompatible.
Apr 27 2012
parent =?UTF-8?B?IuaLlueLl+aVo+atpSI=?= <djj shumtn.com> writes:
On Friday, 27 April 2012 at 15:04:48 UTC, Matt Peterson wrote:
 On Friday, 27 April 2012 at 13:02:56 UTC, 拖狗散步 wrote:
 On Friday, 27 April 2012 at 01:21:56 UTC, Trass3r wrote:
 export c callback fun:

 alias void function(int id) ConnectedCallBack;
 alias void function(int id, void* data, int len) 
 ReadCallBack;
add extern(C) to be safe
Thank, Trass3r! Finally correct, why have to export these two?
You specified the C calling convention with UnmanagedFunctionPointer(CallingConvention.Cdecl), and extern(C) means use the C calling convention. Otherwise they'll be expecting a function using the D calling convention, which is incompatible.
The original so thank you
Apr 28 2012