|
-
December 14th, 2010, 01:08 AM
#1
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
-
December 14th, 2010, 06:40 AM
#2
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.
-
December 14th, 2010, 09:02 AM
#3
Re: Compiles with code blocks, not with mingw
 Originally Posted by itsmeandnobodyelse
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
-
December 14th, 2010, 09:44 AM
#4
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?
if *Buff is a TCHAR* what do you think is Buff[0] ?
-
December 14th, 2010, 09:50 AM
#5
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.
-
December 14th, 2010, 10:32 AM
#6
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
-
December 14th, 2010, 10:53 AM
#7
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);
-
December 14th, 2010, 11:13 AM
#8
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?
-
December 14th, 2010, 01:11 PM
#9
Re: Compiles with code blocks, not with mingw
 Originally Posted by krosty4782
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++.
-
December 14th, 2010, 06:53 PM
#10
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
-
December 14th, 2010, 08:44 PM
#11
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=%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.
-
December 15th, 2010, 04:34 AM
#12
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.
-
December 15th, 2010, 04:40 AM
#13
Re: Compiles with code blocks, not with mingw
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|