NULL in C++ is essentially zero.
So you can pass any zero value depends on the type you choose:
int: 0
IntPtr: IntPtr.Zero
object: null
......
So you can pass any zero value depends on the type you choose:
int: 0
IntPtr: IntPtr.Zero
object: null
......
--------------------------------------------------------------------------
参数“3” : 无法从“System.IntPtr”转换为“KawaiiFTP.KawaiiFTPcls.fd_set”
--------------------------------------------------------------------------忘了和大家说,fd_set和timeval是结构。
会不会和select函数声明有关,我试了两种方法,都不行。大家帮看看哪不对:
---------------------------------------------
[DllImport("wsock32.dll")]
public static extern int select(
int nfds,
[MarshalAs(UnmanagedType.LPStruct)]
fd_set readfds,
[MarshalAs(UnmanagedType.LPStruct)]
fd_set writefds,
[MarshalAs(UnmanagedType.LPStruct)]
fd_set exceptfds,
[MarshalAs(UnmanagedType.LPStruct)]
timeval timeout
);
---------------------------------------------
[DllImport("wsock32.dll")]
public static extern int select(
int nfds,
ref fd_set readfds,
ref fd_set writefds,
ref fd_set exceptfds,
ref timeval timeout
);
---------------------------------------------请各位帮忙啊,只有正确声明了函数,才谈得上调用吧。
ref missing
----------------------------------------------------------
object missing = System.Reflection.Missing.Value;
select(0,ref fdread,ref missing,ref missing,ref missing);[DllImport("wsock32.dll")]
public static extern int select(
int nfds,
ref fd_set readfds,
ref fd_set writefds,
ref fd_set exceptfds,
ref timeval timeout
);
----------------------------------------------------------
报错如下:
参数“3” : 无法从“ref object”转换为“ref KawaiiFTP.KawaiiFTPcls.fd_set”
参数“4” : 无法从“ref object”转换为“ref KawaiiFTP.KawaiiFTPcls.fd_set”
参数“5” : 无法从“ref object”转换为“ref KawaiiFTP.KawaiiFTPcls.timeval”
----------------------------------------------------------究竟该怎么办啊
object myNull = null;
select(0,ref fdread,ref myNull,ref myNull,ref myNull);
试了
object myNull = null;
select(0,ref fdread,ref myNull,ref myNull,ref myNull);
不行,同样的问题
----------------------------------------------------------
报错如下:
参数“3” : 无法从“ref object”转换为“ref KawaiiFTP.KawaiiFTPcls.fd_set”
参数“4” : 无法从“ref object”转换为“ref KawaiiFTP.KawaiiFTPcls.fd_set”
参数“5” : 无法从“ref object”转换为“ref KawaiiFTP.KawaiiFTPcls.timeval”
----------------------------------------------------------fd_set,timeval是结构类型,该用什么来传递空值呢。
object nullObj = tmpStr;
ref nullObj
int a;
}
static unsafe void UseStruct(fd_set *fs){
if(fs == null)
Console.WriteLine("Null used");
else
Console.WriteLine("Instance used");
}
static unsafe void Main(){
UseStruct(null);
}
}
Here are your choices:
1) declear fr_set as class (Ref type).
2) declear select function as: (no ref parameter)
[DllImport("wsock32.dll")]
public static extern int select(int nfds, fd_set readfds, fd_set writefds,
fd_set exceptfds, timeval timeout);
3) then simply pass null as the parameter.OR1) keep fd_set as struct and declear select as:
[DllImport("wsock32.dll")]
public static extern int select(int nfds, IntPtr readfds, IntPtr writefds,
IntPtr exceptfds, timeval timeout);2) Use Marshal class (AllocHGlobal, StructureToPtr) to convert fd_set to an IntPtr. Then pass it as parameter.
你的两个方法我觉得都是可行的,但由于我对C#不很熟悉,在实际编程中有些问题请教如下:
===================================================================
关于方法一:
我这样定义fd_set类
----------------------------------------------------------
public class fd_set
{
public uint fd_count;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = FD_SETSIZE)]
public uint[] fd_array;
}
public class timeval
{
public long tv_sec;
public long tv_usec;
}
----------------------------------------------------------
调用方法如下:
----------------------------------------------------------
select(0,fdread,null,null,timeout)
----------------------------------------------------------
运行时错误信息:
----------------------------------------------------------
无法封送 parameter #5:该类型的类型定义没有布局信息。
----------------------------------------------------------
请指点指点,错在哪里啊。===================================================================
方法二:是用AllocHGlobal(Int)时,Int应该取值多少比较合适?就是划分多大的内存空间?
[StructLayout(LayoutKind.Sequential)]
public class fd_set
{
//.....
}
//timeval也是一样。2. 是用AllocHGlobal(Int)时,Int应该取值多少比较合适?就是划分多大的内存空间?
Marshal.SizeOf(typeof(fd_set));