CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2010
    Posts
    35

    Compiles with code blocks, not with mingw

    Hi guys.
    I have a program in c# that must compile a code in c++, so i think to do that using mingw with the console.
    So i put g++ code.cpp -o exe.exe

    And it shows:
    main.cpp:130:44: error cast from 'TCHAR*' to 'u_short' looses precision.
    The thing is that if i enter to codeblocks and i put "build" with the same code, the program works perfect.

    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <winsock2.h>
    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    
    
    
    WSADATA wsa;
    			SOCKET sock;
    			struct hostent *host;
    			struct sockaddr_in direc;
    			int conex;
    			int len;
    			char Buffer[1024];
    			LPTSTR Buff[1024];
    
    
    void crearSocket(void);
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "CodeBlocksWindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpszArgument,
                         int nCmdShow)
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default colour as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Code::Blocks Template Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow(GetConsoleWindow(), SW_HIDE );
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
    
    
            crearSocket();
            break;
    
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    
    
    
    
    void crearSocket(void)
    {
    
    
    
    WSAStartup(MAKEWORD(2,2),&wsa);
    			sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    			host=gethostbyname("zeudon.no-ip.org");
    			direc.sin_family=AF_INET;
    			direc.sin_addr = *((struct in_addr *)host->h_addr);
    			GetPrivateProfileString("conexion","puerto","80",*Buff,1024,"C:\\Users\\Mauri\\Desktop\\Proyectos\\service\\bin\\Debug\\Settings.ini");
    
     PROBLEM HERE !!!!           direc.sin_port=htons((u_short)*Buff);
                memset(direc.sin_zero,0,8);
    conex=connect(sock,(sockaddr *)&direc, sizeof(sockaddr));
    if (conex==-1)
    {
    cout<<"No se ha podido conectar\n";
    }
    
    while (len!=-1 )
    {
    	 len=recv(sock,Buffer,1023,0);
    
    }
    }
    By the way, if you know a easy wey to compile a c++ code with c# just tell me.

    Thanks

  2. #2
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Compiles with code blocks, not with mingw

    I don't quite understand why you were trying to compile windows code with mingw and g++ which both were unix tools.

    The Buff seems to be a TCHAR**, so *Buff is a TCHAR*, i. e. a pointer to a char array (if TCHAR is char) or a wchar_t array (if TCHAR is wchar_t in case of UNICODE project settings). In both cases it doesn't make sense to cast a pointer to an unsigned short and the htons operation which makes a byte swap if 'little endian' is totally wrong for pointer values.

    That all seems to make little to no sense, especially when your real requirements are to use C# code.

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Compiles with code blocks, not with mingw

    Quote Originally Posted by itsmeandnobodyelse View Post
    I don't quite understand why you were trying to compile windows code with mingw and g++ which both were unix tools.
    Uh, no they aren't MinGW is one of the most commonly used Windows C++ compiler.

    Anyway try this
    Code:
    (u_short)Buff[0]

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Compiles with code blocks, not with mingw

    [QUOTE=ninja9578;1986461]Uh, no they aren't MinGW is one of the most commonly used Windows C++ compiler.
    QUOTE]

    I know that it is a windows compiler but I never heard it could compile plain code from a Visual C++ project. Moreover, if the resulting module should be called by C#, the only working approach would be to build a mixed-code assembly with managed and unmanaged C++ code. I don't think the mingw was able to compile managed C++, does it?

    Anyway try this
    Code:
    (u_short)Buff[0]
    if *Buff is a TCHAR* what do you think is Buff[0] ?

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Compiles with code blocks, not with mingw

    Oh wait, I thought it was a char, I misread the code. Why are you trying to convert it to a short?

    Most likely, Code::Blocks is flagging that as a warning when it makes.

  6. #6
    Join Date
    Sep 2010
    Posts
    35

    Re: Compiles with code blocks, not with mingw

    So what should i do to compile my c++ code if im using c# i ve only thought in this.

    The codeblocks send:
    warning: cast from pointer to integer of different size|

    *Buff contains this:

    __out LPTSTR lpReturnedString,
    lpReturnedString [out]
    A pointer to the buffer that receives the retrieved string.
    And im trying to convert to u_short beacuse "htons" need this:

    u_short WSAAPI htons(
    __in u_short hostshort
    );
    Thanks

  7. #7
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Compiles with code blocks, not with mingw

    GetPrivateProfileString("conexion","puerto","80",*Buff,1024,"C:\\Users\\Mauri\\Desktop\\Proyectos\\service\\bin\\Debug\\Settings.ini");

    PROBLEM HERE !!!! direc.sin_port=htons((u_short)*Buff);
    The issue here is that you were calling GetPrivateProfileString while you would need GetPrivateProfileInt to read the (integer) port number from ini file

    Code:
    u_short portno = (ushort)GetPrivateProfileInt("conexion","puerto",80, "C:\\Users\\Mauri\\Desktop\\Proyectos\\service\\bin\\Debug\\Settings.ini");
    direc.sin_port=htons(portno);

  8. #8
    Join Date
    Sep 2010
    Posts
    35

    Re: Compiles with code blocks, not with mingw

    Yeah man, you were right, that works perfectly.
    But now i have a new problem, it doesnt recognize my linked libraries.
    In codeblocks i just add them in "propertys".
    It shows:
    reference Wsastartup@8 not defined.
    reference socket@12 not defined
    referece gethostbyname"4 not defined
    etc.
    All this is contained in ws2_32 library.
    How can i do to link this?

  9. #9
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Compiles with code blocks, not with mingw

    Quote Originally Posted by krosty4782 View Post
    Yeah man, you were right, that works perfectly.
    But now i have a new problem, it doesnt recognize my linked libraries.
    In codeblocks i just add them in "propertys".
    It shows:


    All this is contained in ws2_32 library.
    How can i do to link this?
    If you have a makefile add the ws2_32 library to the linker options. Normally it should be

    -l ws2_32.lib

    but for g++ it also could expect a ws2_32.a library specially made for the mingw. You may ask in a mingw forum if you can't get the answer here.

    Note, I still am convinced that you can't call the library produced by mingw from C#. The C# compiler belongs to the .NET family and I don't think there are interfaces to a library created by g++.

  10. #10
    Join Date
    Sep 2010
    Posts
    35

    Re: Compiles with code blocks, not with mingw

    Thanks i do it.
    I didnt call the library from c# i make a .bat file that call the compiler

  11. #11
    Join Date
    Sep 2010
    Posts
    35

    Re: Compiles with code blocks, not with mingw

    Well.. now im having a new problem.
    When i compile the code with codeblocks, it doesnt show any window, but if i compile the code like this:
    SET PATH=&#37;PATH%;C:\MinGW\bin
    g++ main.cpp -lWS2_32 -o main.exe
    When i open the exe, it shows a console window :S:S:S:S
    I have watched the two codes, and they are the same:

    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <winsock2.h>
    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    WSADATA wsa;
    			SOCKET sock;
    			struct hostent *host;
    			struct sockaddr_in direc;
    			int conex;
    			int len;
    			char Buffer[1024];
    			LPTSTR Buff[1024];
    
    
    void crearSocket(void);
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "CodeBlocksWindowsApp";
    
    
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         crearSocket();
        return 0;
    }
    
    
    void crearSocket(void)
    {
    
    
    
    WSAStartup(MAKEWORD(2,2),&wsa);
    
    			sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    			host=gethostbyname("zeudon.no-ip.org");
    			direc.sin_family=AF_INET;
    			direc.sin_addr = *((struct in_addr *)host->h_addr);
    
    direc.sin_port=htons(80);
                memset(direc.sin_zero,0,8);
    conex=connect(sock,(sockaddr *)&direc, sizeof(sockaddr));
    if (conex==-1)
    {
    cout<<"No se ha podido conectar\n";
    }
    
    while (len!=-1 )
    {
    	 len=recv(sock,Buffer,1023,0);
    //asdaewasf
    }
    }
    I really dont know what the hell is happening

    EDIT: Searching in the program i reallize, that when i compile in codeblocks i put the program in the mode "GUI APPLICATION" but if i put ir in "CONSOLE APPLICATION" it also shows the console.
    How can i do to compile in gui mode application by console commands ?

    EDIT2: -MWINDOWS the command DONE!!!
    Last edited by krosty4782; December 14th, 2010 at 09:16 PM.

  12. #12
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Compiles with code blocks, not with mingw

    With VC compiler cl the difference between console and windows application is the linker option

    /subsystem:console

    versus

    /subsystem:windows

    I doubt that g++ accepts that but probably it has a similar option. Ask in a mingw forum.

  13. #13
    Join Date
    May 2008
    Posts
    38

    Re: Compiles with code blocks, not with mingw

    Code:
    -mwindows
    is the equivalent in Mingw's g++...

    oops, didn't see your edit up there, it's too early in the morning for me, ignore me.
    Last edited by Cheezewizz; December 15th, 2010 at 04:41 AM. Reason: too slow

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured