Link to home
Start Free TrialLog in
Avatar of NetworkArchitek
NetworkArchitek

asked on

Convert Kernel Device Path to Regular Path (Just string parsing)

I call GetProcessImageFileName and it returns a string like:

"\Device\HarddiskVolume1\Program Files\Internet Explorer\iexplore.exe"

I need the regular path, so it should be c:\Program Files\..." etc.

I have a function that will call QueryDosDevice and convert "\Device\HarddiskVolume1" to "C:". However, my function won't accept the full path, just the "\Device\HarddiskVolume1" portion. My string is a LPTSTR, if someone  could write something simple to conver this that would be great.

Also, I know about the GetModuleFileNathEx and it doesn't really work properly, so I need to use this method.
Avatar of Axter
Axter
Flag of United States of America image

Do a character search for a third '\\' character, and set it to zero before passing it to QueryDosDevice

TCHAR *Pos1 = _tcschr(mypath, '\\');

if (Pos1)
{
TCHAR *Pos2 = _tcschr(Pos1 , '\\');
if (Pos2)
{
TCHAR *Pos3 = _tcschr(Pos2 , '\\');
if (Pos3)
{
 Pos3[0] = 0;
 //Now pas mypath to QueryDosDevice
>>Also, I know about the GetModuleFileNathEx and it doesn't really work properly, so I need to use this method.

Exactly how does it not work?
Please give details.
Avatar of NetworkArchitek
NetworkArchitek

ASKER

I'll try your solution. Well the problem with GetModuleFileNameEx is that if the process is svchost or wuaclt.exe it seems to return a null path.
That doesn't seem to work. I had to change it a bit (add curly braces etc) to get it to compile but Pos1, Pos2, and Pos3 end up pointing to exactly the same strings so when you do a Pos3[0] = 0; it just makes everything null.

My original string is held in LPTSTR and I don't call QueryDosDevice directly. I call a function called GetDrive. So if GetDrive has the signature:

BOOL GetDrive( LPCTSTR pszDosName, CString& outputDriveLetter )

 And my original string is held in a variable called lpPath, what's the best way to accomplish this? Thanks in advance.
>>That doesn't seem to work. I had to change it a bit (add curly braces etc) to get it to compile but Pos1, Pos2, and Pos3 end up pointing to exactly the same
>>strings so when you do a Pos3[0] = 0; it just makes everything null.
Yes, I forgot to increment it before the next search.
Try the following:
CHAR *Pos = _tcschr(mypath, '\\');

if (Pos)
{
Pos = _tcschr((++Pos) , '\\');
if (Pos)
{
Pos = _tcschr((++Pos), '\\');
if (Pos)
{
 Pos[0] = 0;
 //Now pas mypath to QueryDosDevice
>>I'll try your solution. Well the problem with GetModuleFileNameEx is that if the process is svchost or wuaclt.exe it seems to return a null path.

I would still use this as the primary method, and then use the more complex method if you get a NULL path from this API.
Ok wonderful! One more issue and then I'll accept. Of course, I'm losing my original path. Here is my code:

---------------------
TCHAR *Pos1 = _tcschr(lpPath, '\\');
TCHAR *Pos2, *Pos3;
if (Pos1)      
{
TCHAR *Pos2 = _tcschr(++Pos1 , '\\');
if (Pos2)
{
TCHAR *Pos3 = _tcschr(++Pos2 , '\\');
f (Pos3)
Pos3[0] = 0;
}
}
                              
CString csTemp2;
GetDrive(lpPath, csTemp2);
output += csTemp2;
--------------

output is a CString. Could you modify it so that output becomes the correct path. i.e. "C:\Program Files\..." blah blah.  Just to be clear, csTemp2 after the GetDrive call has "C:" as its value. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial