RegLoadMUIString Vista P-Invoke

G

Guest

Hello,
I'm trying to get an MUI string out of the registry in display friendly
format. From what I've read, strings in the following format:
“@[path]\dllname,-strID†are MUI strings and receive special handling via
the RegLoadMUIString API call.

This is what I have come up with for the call:
[DllImport("advapi32.dll")]
internal static extern long RegLoadMUIString(IntPtr hKey, string pszValue,
StringBuilder pszOutBuf, int cbOutBuf, out int pcbData, uint Flags, string
pszDirectory);

I'm calling this function with a valid pointer to an open registry key,
passing in the appropriate value key (pszValue) which has the MUI formatted
string. When I check the output buffer (pszOutBuf) it's always an empty
string.

Has anyone been able to get this call to work? I cannot find any examples
on the web.

thanks,
-bp
 
G

Guest

Below is full code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace ConsoleApplication2
{
class Program
{
[DllImport("advapi32.dll")]
public static extern long RegOpenKeyEx(IntPtr hKey, string
lpSubKey,int ulOptions,int samDesired, out IntPtr phkResult);

[DllImport("advapi32.dll")]
internal static extern long RegLoadMUIString(IntPtr hKey, string
pszValue, StringBuilder pszOutBuf, int cbOutBuf, out int pcbData, uint Flags,
string pszDirectory);

[DllImport("advapi32.dll")]
public static extern int RegCloseKey(IntPtr hKey);


static void Main(string[] args)
{
try
{
//NOTE: Testing Vista MUI Registry strings

//NOTE: Pointer to HKEYLM
IntPtr localMachine = new
IntPtr((long)unchecked((int)0x80000002));

//NOTE: regKey will contain the pointer to the open registry
key
IntPtr regKey;

int pcbData = 0;

//NOTE: Open a device key with KEY_READ access rights.
RegOpenKeyEx(localMachine,
@"SYSTEM\CurrentControlSet\Control\Class\{36FC9E60-C465-11CF-8056-444553540000}", 0, 0x20019, out regKey);

//NOTE: Build the output buffer reference
StringBuilder lptStr = new StringBuilder(1024);

//NOTE: ClassDesc contains the MUI formatted string
RegLoadMUIString(regKey, "ClassDesc", lptStr, 1024, out
pcbData, 0, null);

//NOTE: Close the key
RegCloseKey(regKey);

//NOTE: Output values to console
Console.WriteLine("Reg key : " + regKey.ToString());
Console.WriteLine("LPTSTR : " + lptStr.ToString());
Console.WriteLine("PCBDATA : " + pcbData.ToString());
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception : " + ex.Message);
Console.ReadLine();
}
}
}
}
 
M

Mattias Sjögren

[DllImport("advapi32.dll")]
public static extern long RegOpenKeyEx(IntPtr hKey, string
lpSubKey,int ulOptions,int samDesired, out IntPtr phkResult);

[DllImport("advapi32.dll")]
internal static extern long RegLoadMUIString(IntPtr hKey, string
pszValue, StringBuilder pszOutBuf, int cbOutBuf, out int pcbData, uint Flags,
string pszDirectory);

The return type for both functions should be int, not long.


Mattias
 
G

Guest

Changing the return types to int had no affect on the outcome of the call.

-brett

Mattias Sjögren said:
[DllImport("advapi32.dll")]
public static extern long RegOpenKeyEx(IntPtr hKey, string
lpSubKey,int ulOptions,int samDesired, out IntPtr phkResult);

[DllImport("advapi32.dll")]
internal static extern long RegLoadMUIString(IntPtr hKey, string
pszValue, StringBuilder pszOutBuf, int cbOutBuf, out int pcbData, uint Flags,
string pszDirectory);

The return type for both functions should be int, not long.


Mattias
 
W

Willy Denoyette [MVP]

And what does RegLoadMUIString call return? You don't check the return value
in your code!!!

Willy.


| Changing the return types to int had no affect on the outcome of the call.
|
| -brett
|
| "Mattias Sjögren" wrote:
|
| > > [DllImport("advapi32.dll")]
| > > public static extern long RegOpenKeyEx(IntPtr hKey, string
| > >lpSubKey,int ulOptions,int samDesired, out IntPtr phkResult);
| > >
| > > [DllImport("advapi32.dll")]
| > > internal static extern long RegLoadMUIString(IntPtr hKey,
string
| > >pszValue, StringBuilder pszOutBuf, int cbOutBuf, out int pcbData, uint
Flags,
| > >string pszDirectory);
| >
| > The return type for both functions should be int, not long.
| >
| >
| > Mattias
| >
| > --
| > Mattias Sjögren [C# MVP] mattias @ mvps.org
| > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > Please reply only to the newsgroup.
| >
 
W

Willy Denoyette [MVP]

First thing to do after calling any of the Win32 API's is checking the
return value, this value indicates success or failure. If the return is
success , you can use the argument values returned, if the return value
indicates a failure, you have to inspect the error code or you need to call
GetLastWin32Error, this depends on the API, that's why you need to check the
API docs first.

In your case 120 means: This function is not supported on this system.
Are you sure you run this on Vista?, the API isn't implemented on anything
lower.

Willy.



| Well, I know the call is not working because the variable I care about:
| pszOutBuf is an empty string. The integer that is returned from the
function
| call is: 120.
|
| I really need someone to respond that has used the RegLoadMUIString
function.
|
| Here is the function delaration:
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regloadmuistring.asp
|
| thanks,
| -bp
|
| "Willy Denoyette [MVP]" wrote:
|
| > And what does RegLoadMUIString call return? You don't check the return
value
| > in your code!!!
| >
| > Willy.
| >
| >
| > | > | Changing the return types to int had no affect on the outcome of the
call.
| > |
| > | -brett
| > |
| > | "Mattias SjÃf¶gren" wrote:
| > |
| > | > > [DllImport("advapi32.dll")]
| > | > > public static extern long RegOpenKeyEx(IntPtr hKey, string
| > | > >lpSubKey,int ulOptions,int samDesired, out IntPtr phkResult);
| > | > >
| > | > > [DllImport("advapi32.dll")]
| > | > > internal static extern long RegLoadMUIString(IntPtr hKey,
| > string
| > | > >pszValue, StringBuilder pszOutBuf, int cbOutBuf, out int pcbData,
uint
| > Flags,
| > | > >string pszDirectory);
| > | >
| > | > The return type for both functions should be int, not long.
| > | >
| > | >
| > | > Mattias
| > | >
| > | > --
| > | > Mattias SjÃf¶gren [C# MVP] mattias @ mvps.org
| > | > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > | > Please reply only to the newsgroup.
| > | >
| >
| >
| >
 
G

Guest

I'm not going to sit in here and talk best practices with you; I simply want
to get my code working.

Do you really think I don't know what OS I'm using?

Where did you find the documentation on the 120 error code? Maybe it has to
do with the signature being incorrect, however I'm pretty sure it's right.

-bp
 
W

Willy Denoyette [MVP]

| I'm not going to sit in here and talk best practices with you; I simply
want
| to get my code working.
|

If you did apply these best practices and searched the API description in
the SDK doc's for the
signature and the meaning of the return value in you wouldn't even be here.
Anyway, this API isn't yet implemented on the RC1 build, don't know about
RC2 though.

Willy.
 
G

Guest

Thanks for looking into this for me Willie and sorry about the last post.
I've been a bit frustrated trying to figure out the problem.

-bp
 
W

Willy Denoyette [MVP]

No offence taken, I understand, anyway I would suggest you post Vista issues
to the Vista forums, for instance SDK related issues can be posted to
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=121&SiteID=1, and I
would also suggest you to report an issue to the feedback site at :
http://connect.microsoft.com:80/feedback/default.aspx?SiteID=210

Willy.

and it's Willy ;-)

| Thanks for looking into this for me Willie and sorry about the last post.
| I've been a bit frustrated trying to figure out the problem.
|
| -bp
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > | > | I'm not going to sit in here and talk best practices with you; I
simply
| > want
| > | to get my code working.
| > |
| >
| > If you did apply these best practices and searched the API description
in
| > the SDK doc's for the
| > signature and the meaning of the return value in you wouldn't even be
here.
| > Anyway, this API isn't yet implemented on the RC1 build, don't know
about
| > RC2 though.
| >
| > Willy.
| >
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top