-1

I create a thread from the main thread

void xui(PLDR_DATA_TABLE_ENTRY ModuleHandle){  
ExCreateThread(&moduleHandle, 0, &threadId, ConnectSock , (LPTHREAD_START_ROUTINE)ConnectSock, NULL, 0x02);
}

the thread called

DWORD WINAPI ConnectSock() {
SOCKET Sock = NetDll_socket(XNCALLER_SYSAPP, AF_INET, SOCK_STREAM, IPPROTO_TCP);
    BYTE IPAddress[0x4] =  { 34, 231, 248, 251 };
    WORD Port = 6667;
    DWORD SocketError;
    XNetStartupParams xnsp;
    WSADATA WsaData;
    BOOL SockOpt = TRUE;
    DWORD sendRecvSize = 1024;
    sockaddr_in httpServerAdd;
    httpServerAdd.sin_addr.S_un.S_addr = *(PDWORD)IPAddress;
    //httpServerAdd.sin_addr.s_addr = inet_addr("tha-row.net");
    httpServerAdd.sin_port = htons(Port);
    httpServerAdd.sin_family = AF_INET;
    ZeroMemory(&xnsp, sizeof(xnsp));

    // Configure our xnsp variable
    xnsp.cfgSizeOfStruct = sizeof(XNetStartupParams);
    xnsp.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;
    // Safely startup XNet
    if ((SocketError = NetDll_XNetStartup(XNCALLER_SYSAPP, &xnsp)) != S_OK) {

    }

    // Safely startup WSA
    if ((SocketError = NetDll_WSAStartupEx(XNCALLER_SYSAPP, MAKEWORD(2, 2), &WsaData, 2)) != S_OK) {

    }

    // Safely create socket
    //Sock = NetDll_socket(XNCALLER_SYSAPP, AF_INET, SOCK_STREAM, IPPROTO_TCP);
    //Sock = socket(AF_INET, SOCK_STREAM, 0);
    // Disable network encryption
    if (NetDll_setsockopt(XNCALLER_SYSAPP, Sock, SOL_SOCKET, 0x5801, (PCSTR)&SockOpt, 4) != S_OK) {

    }

    // Configure socket send/recv size
    NetDll_setsockopt(XNCALLER_SYSAPP, Sock, SOL_SOCKET, SO_SNDBUF, (PCSTR)&sendRecvSize, 4);
    NetDll_setsockopt(XNCALLER_SYSAPP, Sock, SOL_SOCKET, SO_RCVBUF, (PCSTR)&sendRecvSize, 4);

    // Create connection timeout
    struct timeval tv;
    tv.tv_sec = 15;
    tv.tv_usec = 0;
    setsockopt(Sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));

    if (NetDll_connect(XNCALLER_SYSAPP, Sock, (struct sockaddr*)&httpServerAdd, sizeof(httpServerAdd)) == SOCKET_ERROR) {
        std::string wsacon("subtitle_add \"fai!");
        wsacon += "\" 3";
        const char* Nwsacon = wsacon.c_str();
        ConsoleCommand2(Nwsacon);
    }
    int ReadBytes = 0;
    ofstream ircdata;
    char buff[1024];
    while (true)
    {
        ZeroMemory(buff, 1024);

        int bytesReceived = NetDll_recv(XNCALLER_SYSAPP,Params->sock, buff, 1024, 0);
        if (bytesReceived == SOCKET_ERROR)
        {

        }
        if (bytesReceived > 0) {
        }
    }
    NetDll_closesocket(XNCALLER_SYSAPP, Sock);
}

how can i access "Sock" from outside the new created thread and send a message like

string string2send = "string to send\n";
                            NetDll_send(XNCALLER_SYSAPP, Sock,string2send.c_str(), string2send.length(), 0);

note this is not the entire code of the socket i shortened it as much as i can to get my point across..

I just want to send to the socket from another thread

if i have

SOCKET Sock = NetDll_socket(XNCALLER_SYSAPP, AF_INET, SOCK_STREAM, IPPROTO_TCP);

as a global variable Sock still equals 0 outside of the thread

ravi_elite
  • 11
  • 6
  • 1
    Unrelated recommendation: Go all-in on C++ and save `malloc` for the rare edge cases where stupid, bare-bones memory management makes sense. This might be a job for `new`, but I'd still try to find a way to deploy a smart pointer. – user4581301 Feb 15 '22 at 01:34
  • By the way, what's the trouble? I can think of many things that could be going wrong here, and that's pretty serious lack of focus. Clearly describe the behaviour you want and the behaviour you get and strongly consider constructing a [mre] because in sockets and threads the nitty-gritty details matter. – user4581301 Feb 15 '22 at 01:37
  • @user4581301 i am trying to pass a socket to a new thread so i can recv on that thread updating code with what i really have – ravi_elite Feb 15 '22 at 01:46
  • @ravi_elite Where is the code that is creating the thread and passing the struct to it? You didn't show that. Please provide a [mcve]. – Remy Lebeau Feb 15 '22 at 01:47
  • updated the code – ravi_elite Feb 15 '22 at 01:51
  • updated for minimal reproducible example – ravi_elite Feb 15 '22 at 02:20
  • 1
    Consider using C++ threads instead if Windows threads – n. m. could be an AI Feb 15 '22 at 05:34
  • @ravi_elite That is not what I would call **minimal**, but whatever. `ConnectSock` is the wrong thing to pass in the 4th parameter of `ExCreateThread()`, and the 6th parameter is an `LPVOID` (`void*`) not an `LPVOID*` (`void**`). And `ConnectSock()` is using the wrong signature for `LPTHREAD_START_ROUTINE` when passing in a parameter, it needs to be `DWORD CALLBACK ConnectSock(void *socketinfo)` instead. – Remy Lebeau Feb 15 '22 at 05:44
  • updated code to show more clearly what i need.. I just need to access the SOCKET from outside the thread – ravi_elite Feb 16 '22 at 21:37

2 Answers2

0

You have several choices

  • make Sock global
  • Pass in a Sock pointer in the thread start parameters
  • call some method in the connect function passing the Sock as a paramter and start your send thread in that function, passing Sock to it (like in option 2)

in the first two cases you need some synchronization primitives to not use the Socket before its connected.

BTW this is an unusual 'shape' for code dealing with a TCP connection. Normally one thread deals with one connection

pm100
  • 48,078
  • 23
  • 82
  • 145
0

so this worked for me thanks to @Remy and @pm100

void Player::ExternalMsg(){
string Z = "IRCX\r\n";
    NetDll_send(XNCALLER_SYSAPP, Sock2, Z.c_str(), Z.length(), 0);
}
void SendSockInfo(SOCKET s) {
    Sock2 = s;
}

after I made a successful connection

SendSockInfo(sock);
ravi_elite
  • 11
  • 6