0
votes

I have writed a simple wrapper of Winsock in C for C#. When i use Socket.Accept() in some thread and client.Connect in main thread - i sometimes( receive DllNotFoundException.

System.DllNotFoundException: Exception of type 'System.DllNotFoundException' was thrown. at (wrapper managed-to-native) TcpWrapper:ES_ConnectClient (string,int) at TcpClientSocket.Connect (System.String address, Int32 port) [0x00000] in C:...\ESCore\TcpClientSocket.cs:21

TcpClientSocket.Connect calling

[DllImport("ESocket")]
public static extern int ES_ConnectClient(string ip, int port);

I don't know why that happening rarely.

Some code:

listener = new TcpListenerSocket(50231); //calling bind from library here
if (listener.Start()) //calling listen from library
{
    thread = new Thread(new ThreadStart(Listen));
    thread.Start();


    client = new TcpClientSocket();
    if(client.Connect("localhost", 50231)) //exception here!
    {
        ...
        client.Close();
    }
}

Thread code:

void Listen()
{
    while (m_Running)
    {
        if (listener.Pending()) //select from library
        {
            TcpClientSocket socket = listener.Accept(); //accept from library
            if (socket != null)
            {
                ...
                socket.Close();
            }
        }
    }
}

Listener is also in library.

Library code: http://pastie.org/private/hdgl9zqxfjt2arlkj11q

Update: That happens only in Unity3d. In mono project and Microsoft .NET no errors.

1

1 Answers

1
votes

It is a simple "file not found" error message. You never said "and I made sure that the DLL is present" so that's the probable failure mode.

You have to make sure that Windows can find the DLL, it should be present in the same folder as your EXE. Select your C# project. Project + Add Existing Item, select the ESocket.dll file so it is added to your project. Select it and switch to the Properties window. Set the Copy to Output Directory option to "Copy if newer". If the DLL is being built by another project in your solution then be sure to set the project dependency so that project is always built first. Rebuild your project.

This ensures that ESocket.dll is always present in the build directory and that Windows can always find it.