![]() |
|
[C] Is it normal in C++ to be constantly converting variable types? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Is it normal in C++ to be constantly converting variable types? (/showthread.php?tid=6070626) |
Is it normal in C++ to be constantly converting variable types? - gurlhackerclub - Apr 23, 2020 Is it normal in C++ to be constantly converting variable types? For instance, I want to get the computer name and send that variable through a socket. I find the function GetComputerName which gives me what I need in TCHAR. I use the send function and it needs the computername variable in const char. Is it normal to have to be jumping around like this? Here is an example of code. Doesn't work because tchar to const char issue. Code #ifndef WIN32_LEAN_AND_MEANRE: Is it normal in C++ to be constantly converting variable types? - HFDone - Apr 23, 2020 and what if the name is unique characters if you sure its english then ypu can convert it RE: Is it normal in C++ to be constantly converting variable types? - gurlhackerclub - Apr 23, 2020 I think my answer is to learn C#. C++ would be great if I could find working examples. I found an example in C# in about 5 minutes that does what I need. Code using System;RE: Is it normal in C++ to be constantly converting variable types? - dracx - Apr 23, 2020 I would advice to just send the raw data as it is to your server and let the server handle it. Try casting it to (const char *) when passing to the send function. And btw, use the compnameBufSize on the send function, it contains the size/number of TCHARs in the comp name buffer, and TCHAR may be ascii or wchar (or something else) so do some preprocessor statement to properly handle all this stuff. RE: Is it normal in C++ to be constantly converting variable types? - saltycracker - Apr 24, 2020 You'll always have problems lining up data types when you cross api borders. Note: You should invest some time investigating marshalling software if you are serious about server programming. RE: Is it normal in C++ to be constantly converting variable types? - Securifier - Apr 24, 2020 TCHAR is an alias to CHAR or WCHAR, following your compiler configuration. One WCHAR is on 2 bytes, that means sizeof(WCHAR) == sizeof(short). It is UTF16 One CHAR is one byte long. It is UTF8 WCHAR is the standards of Windows, and you should use it. CHAR is the standard on cross-platform C and C++, and Linux. That explains the problems of conversion you are getting. RE: Is it normal in C++ to be constantly converting variable types? - saltycracker - Apr 24, 2020 (Apr 24, 2020 05:04 AM)Securifier Wrote: TCHAR is an alias to CHAR or WCHAR, following your compiler configuration. Linux has CHAR? Do you mean char? RE: Is it normal in C++ to be constantly converting variable types? - Securifier - Apr 24, 2020 (Apr 24, 2020 06:27 AM)saltycracker Wrote: Linux has CHAR? Do you mean char? CHAR is a Windows.h alias of char, it is the same yes. RE: Is it normal in C++ to be constantly converting variable types? - saltycracker - Apr 24, 2020 (Apr 24, 2020 06:28 AM)Securifier Wrote: CHAR is a Windows.h alias of char, it is the same yes. You shouldn't use windows specific headers or source code in Linux. They were never meant to run on other platforms... That's why they are called Windows.h. It would be the same as unistd.h(unix standard header file). You won't expect to use anything in unistd.h in a Windows program bc it was never designed to work on that platform. RE: Is it normal in C++ to be constantly converting variable types? - Securifier - Apr 24, 2020 (Apr 24, 2020 06:36 AM)saltycracker Wrote: You shouldn't use windows specific headers or source code in Linux. They were never meant to run on other platforms... That's why they are called Windows.h. He is programming on Windows. RE: Is it normal in C++ to be constantly converting variable types? - saltycracker - Apr 24, 2020 Here, let me respond by quoting this person. (Apr 24, 2020 05:04 AM)Securifier Wrote: CHAR is the standard on cross-platform C and C++, and Linux. RE: Is it normal in C++ to be constantly converting variable types? - gurlhackerclub - Apr 24, 2020 Much love and thanks for help. I found a different example to get computer name in char format and got it to work. Problem is I don't know how C++ works. I just copy examples. And C++ has too many old examples of code floating around. Code char compname[MAX_COMPUTERNAME_LENGTH + 1];I have no idea why there are three types of functions for everything. GetComputerNameEx, GetComputerNameExA, GetComputerNameExW RE: Is it normal in C++ to be constantly converting variable types? - dracx - Apr 25, 2020 (Apr 24, 2020 01:03 PM)gurlhackerclub Wrote: Much love and thanks for help.Man that's sad af, if you dont understand a concept, spend an hour or a day just to understand it, if you think you'll spend too much time on just learning one thing, I assure you that is wrong. Stop copy pasting code and try and understand why this line is this and why is that, then move onto the next line. Code TCHAR szCompName[MAX_COMPUTERNAME_LENGTH + 1];Check this out, I've spared error checking and proper initializations. |