Code question menu

Shout-Out

User Tag List

Results 1 to 3 of 3
  1. #1
    TimDTR's Avatar Member
    Reputation
    1
    Join Date
    May 2008
    Posts
    2
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code question

    I'm trying to get in the habit of making clean/correct code, so i'm asking you guys to let me know if you see anything.. no matter how small it is.. I could change in this simple code. I'm posting this here because there seems to be quite a few people to actually know what they are talking about.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <tlhelp32.h>
    
    using namespace std;
    
    int main()
    {
    
    	HANDLE hProcessSnapShot;
    	PROCESSENTRY32 pProcessEntry;
    
    	cout << "DEBUG: Attempting to create CreateToolhelp32Snapshot()" << endl;
    	hProcessSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
    	
    	if (hProcessSnapShot != INVALID_HANDLE_VALUE) {
    		cout << "DEBUG: hProcessSnapShot = " << hProcessSnapShot << endl;
    	} else {
    		cout << "ERROR: CreateToolhelp32Snapshot() - " << GetLastError() << endl;
    		return 1;
    	}
    
    	cout << "DEBUG: Filling value of pProcessEntry.dwSize" << endl;
    	pProcessEntry.dwSize = sizeof(PROCESSENTRY32);
    	cout << "DEBUG: pProcessEntry.dwSize = " << pProcessEntry.dwSize << endl;
    
    	if (Process32First(hProcessSnapShot, &pProcessEntry))
    	{
    		cout << "DEBUG: Starting process main loop..." << endl;
    	} else {
    		cout << "ERROR: Process32First()" << GetLastError() << endl;
    		return 1;
    	}
    
    	do
    	{
    		cout << "[" << pProcessEntry.szExeFile << "]" << endl;
    	} while (Process32Next(hProcessSnapShot, &pProcessEntry));
    
    	CloseHandle(hProcessSnapShot);
    
    	return 0;
    }

    Code question
  2. #2
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your program isn't Unicode safe. You need to use TCHARs in place of chars or wchar_ts, and wrap your strings in the TEXT macro. Also, you'll want to use a macro to wrap the STL and resolve to either wide or regular versions of each class depending on the compiler flag.

    e.g.
    Code:
    #ifdef _UNICODE
    #define tstring wstring
    #define tfstream wfstream
    #define tifstream wifstream
    #define tofstream wofstream
    #define tstringstream wstringstream
    #define tcout wcout
    #define tcin wcin
    #define tistream wistream
    #define tostream wostream
    #else
    #define tstring string
    #define tfstream fstream
    #define tifstream ifstream
    #define tofstream ofstream
    #define tstringstream stringstream
    #define tcout cout
    #define tcin cin
    #define tistream istream
    #define tostream ostream
    #endif // _UNICODE
    There's a bunch missing from there that you might want but you get the idea.

    Furthermore, your code would potentially leak handles if used in a more complex environment. You should write classes for things like handles so you can use RAII.

    And oh, defining variables at the top of a function is poor C++, you should define them as close to the point of initialisation as possible, and if possible, initialisation should happen at the same time as the object is defined.

    Lastly, your naming scheme is stupid. 'pProcessEntry' implies that the variable is a pointer, which it isn't.

  3. #3
    TimDTR's Avatar Member
    Reputation
    1
    Join Date
    May 2008
    Posts
    2
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for the feedback, exactly what I was looking for

Similar Threads

  1. [PQR] Mage pet coding question
    By ectl2k in forum WoW Bots Questions & Requests
    Replies: 0
    Last Post: 04-24-2013, 09:48 PM
  2. Replies: 3
    Last Post: 02-01-2013, 03:34 AM
  3. Server Code Question
    By kaoskode in forum Programming
    Replies: 3
    Last Post: 12-03-2012, 05:22 PM
  4. Operation code question.
    By lweid in forum WoW Bots Questions & Requests
    Replies: 0
    Last Post: 04-12-2011, 11:27 PM
  5. Interest in php tuts or coding questions?
    By Gravecorp in forum Programming
    Replies: 6
    Last Post: 02-25-2009, 07:10 AM
All times are GMT -5. The time now is 08:31 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search