Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
cannot get it to work with multi byte character set
its working fine with unicode
i tried but wasn't able to convert it to multibye
This is the error i am getting
DWORD dwResult = GetLogicalDriveStrings(MAX_PATH, text); // text = szLogicalDrives
on this line i am getting cannot convert from wchar_t* to LPSTR

UINT nDriveType = GetDriveType(szSingleDrive);
on this line i am getting cannot convert WCHAR to LPCSTR


What I have tried:

#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <string>
#include<iostream>
using namespace std;

#define MAX_LETTER 26
char PREV_DRIVE_LIST[MAX_LETTER];
char NEW_DRIVE_LIST[MAX_LETTER];

/* To GET DRIVE LIST in char ARRAY */
void getUSBStorageDeviceList(char drive[]) {

	int count = 0;

	char szLogicalDrives[MAX_PATH];
	size_t size = strlen(szLogicalDrives) + 1;
	wchar_t* text = new wchar_t[size];

	size_t outSize;
	mbstowcs_s(&outSize, text, size, szLogicalDrives, size - 1);

	DWORD dwResult = GetLogicalDriveStrings(MAX_PATH, text); // text = szLogicalDrives
	WCHAR* szSingleDrive = text;

	while (*szSingleDrive)
	{
		UINT nDriveType = GetDriveType(szSingleDrive);

		//  printf("\nFUNC: getRemovableDisk, Drive Name%d= %s", ++count, szSingleDrive);

		if (nDriveType == DRIVE_UNKNOWN) {
			//  cout << "\nDrive type : Unknown: The drive type cannot be determined." << endl;
		}
		else if (nDriveType == DRIVE_NO_ROOT_DIR) {
			//  cout << "\nDrive type : Invalid Root Directory Media: The root path is invalid." << endl;
		}
		else if (nDriveType == DRIVE_REMOVABLE) {
			//  cout << "\nDrive type :  Removable Media:" << endl;
			char letter = szSingleDrive[0];
			drive[letter - 65] = letter;
		}
		else if (nDriveType == DRIVE_FIXED) {
			//cout << "\nDrive type : Fixed Media: " << endl;
		}
		else if (nDriveType == DRIVE_REMOTE) {
			//cout << "\nDrive type : Remote Media: The drive is a remote (network) drive.." << endl;
		}
		else if (nDriveType == DRIVE_CDROM) {
			//cout << "\nDrive type : CD ROM:   The drive is a CD-ROM drive." << endl;
		}
		else if (nDriveType == DRIVE_RAMDISK) {
			//cout << "\nDrive type : RAM Disk: The drive is a RAM disk." << endl;
		}

		szSingleDrive += wcslen(szSingleDrive) + 1; // next drive 
	}
}

int main(void) {

	int count = 0;
	for (int i = 0; i < MAX_LETTER; i++) {
		PREV_DRIVE_LIST[i] = '0';
		NEW_DRIVE_LIST[i] = '0';
	}
	// initial drive list which is already attached 
	getUSBStorageDeviceList(PREV_DRIVE_LIST);

	while (1) {

		getUSBStorageDeviceList(NEW_DRIVE_LIST);
		count = 1;

		/* Check for insertion and removabal*/

		for (int i = 0; i < MAX_LETTER; i++) {
			// check for new drive
			if ((NEW_DRIVE_LIST[i] >= 65 && NEW_DRIVE_LIST[i] <= 89) && (PREV_DRIVE_LIST[i] == '0')) {

				printf("\nNew Device Inserted%d : %c", count++, NEW_DRIVE_LIST[i]);
				PREV_DRIVE_LIST[i] = NEW_DRIVE_LIST[i];
			}
		}
		// fill ALl zero 
		for (int i = 0; i < MAX_LETTER; i++) {
			NEW_DRIVE_LIST[i] = '0';
		}
		// update NEW drive list
		getUSBStorageDeviceList(NEW_DRIVE_LIST);

		for (int i = 0; i < MAX_LETTER; i++) {
			// check for removed drive
			if ((PREV_DRIVE_LIST[i] >= 65 && PREV_DRIVE_LIST[i] <= 89) && (NEW_DRIVE_LIST[i] == '0')) {
				printf("\nDevice Removed%d : %c", count++, PREV_DRIVE_LIST[i]);
				PREV_DRIVE_LIST[i] = NEW_DRIVE_LIST[i];
			}
		}
		Sleep(500);
	}

	return 0;
}
Posted
Updated 3-Mar-20 0:30am
v2
Comments
Richard MacCutchan 3-Mar-20 5:12am    
What is not working? Please provide proper details of the problem.

1 solution

C++
char szLogicalDrives[MAX_PATH];
size_t size = strlen(szLogicalDrives) + 1;
wchar_t* text = new wchar_t[size];

You cannot get a valid answer from strlen called on an uninitialised array. You may get zero, or some random number. Either way, the rest of the code will not give valid results.
 
Share this answer
 
Comments
Member 12899279 3-Mar-20 5:49am    
so is there any solution?you should ave posted it in comment and not solution because its not
no offense
Richard MacCutchan 3-Mar-20 6:01am    
Solution to what? This code is just not valid, so you need to correct it first.

As to your question, you should not need to convert Unicode to multi-byte. Use the TCHAR and _t... functions for all character types, and generate either a Unicode or multi-byte version of your program. Using the proper macros will allow you easily to switch types.
Member 12899279 3-Mar-20 6:11am    
DWORD dwResult = GetLogicalDriveStrings(MAX_PATH, text); // text = szLogicalDrives
on this line i am getting cannot convert from wchar_t* to LPSTR

UINT nDriveType = GetDriveType(szSingleDrive);
on this line i am getting cannot convert WCHAR to LPCSTR
Richard MacCutchan 3-Mar-20 8:42am    
You are getting into a confused mess with your code. Get rid of all wchar_t references and stick to simple ASCII. Once you have the program working then you can think about creating a Unicode version, although I cannot see what advantage that would give you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900