dll - Pinvoke / call native Windows API function from C# -


from within c#-application use functions of external dll. dll uses network functions. necessary initialise winsock out of c#-application let network sockets work dll. that's how try execute wsastartup initialisation, not seem work:

class program {     [structlayout(layoutkind.sequential, charset = charset.unicode)]     internal struct wsadata     {         internal int16 version;         internal int16 highversion;          [marshalas(unmanagedtype.byvaltstr, sizeconst = 257)]         internal string description;          [marshalas(unmanagedtype.byvaltstr, sizeconst = 129)]         internal string systemstatus;          internal int16 maxsockets;         internal int16 maxudpdg;         internal intptr vendorinfo;     }      [dllimport("ws2_32.dll", charset = charset.auto, setlasterror = true)]     static extern int32 wsastartup(int16 wversionrequested, out wsadata wsadata);      static void main(string[] args)     {         wsadata wsadata;          wsadata.version = 0;         wsadata.highversion = 0;         wsastartup(0,out wsadata); // initialise windows socket          ... // calling external dll functions     } } 

wsastartup() seems called used dll still not able access network. seems wsastartup() did not work reason. ideas i'm doing wrong here?

kind regards

michael

you must pass 2 << 8 | 2 fist parameter (it version requested wsa)

note there small bug in signature microsoft produced (see is .net use of wsastartup safe 64-bit apps?), isn't problem, can ignore it.

you should check return values of api functions call:

int resp = wsastartup(2 << 8 | 2, out wsadata); // initialise windows socket  if (resp != 0) {     // error } 

technically check suggested microsoft in https://msdn.microsoft.com/library/windows/desktop/ms742213.aspx more complex, because do:

if (resp != 0 || wsadata.version != (2 << 8 | 2)) {     // error } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -