We help IT Professionals succeed at work.

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

NetworkArchitek
on
1,124 Views
Last Modified: 2013-12-14
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.
Comment
Watch Question

AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
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
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
>>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.

Author

Commented:
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.

Author

Commented:
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.
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
>>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
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
>>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.

Author

Commented:
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.
Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.