Missing GetEntryAssembly() again

  • Thread starter Thread starter Ralf Heitmann
  • Start date Start date
R

Ralf Heitmann

Hi there,

I need to find out the "Entry Assembly" in CompactFramework.
The full FX implements "Assembly.GetEntryAssembly()" for that case.

I also found in this newsgroup a hint to implement this code
(GetModuleFileName with P/Invoke).

[DllImport("CoreDll.dll", SetLastError=true)]
public static extern IntPtr GetModuleHandle(IntPtr ModuleName);
[DllImport("CoreDll.dll", SetLastError=true)]
public static extern Int32 GetModuleFileName(IntPtr hModule, StringBuilder
ModuleName, Int32 cch);

private static string GetEntryAssembly()
{
StringBuilder sb = null;
IntPtr hModule = GetModuleHandle(IntPtr.Zero);
if (IntPtr.Zero != hModule)
{
sb = new StringBuilder(255);
if (0 == GetModuleFileName(hModule, sb, sb.Capacity))
{
sb = null;
}
}
return sb.ToString();
}

But the important Drawback of this solution is that this code doesn't work
when I start the Executable on WindowsXP.
1. Is there any other alternative to get the Entry Assembly?
2. How can get information about the running OS (WindowsXP) when I execute
my CF code?

Anyone done this?

Thanks Ralf
 
Ralf, try using the following. Basically have a DllImport for both the CE
and XP versions of GetModuleHandle (and GetModuleFileName). Wrap them both
in a separate function that trys one, catches a MissingMethodException and
then tries the other.

[DllImport("CoreDll.dll", SetLastError=true,
EntryPoint="GetModuleHandle")]
private static extern IntPtr CE_GetModuleHandle(IntPtr ModuleName);
[DllImport("CoreDll.dll", SetLastError=true,
EntryPoint="GetModuleFileName")]
private static extern Int32 CE_GetModuleFileName(IntPtr hModule,
StringBuilder
ModuleName, Int32 cch);
[DllImport("Kernel32.dll", SetLastError=true,
EntryPoint="GetModuleHandle")]
private static extern IntPtr XP_GetModuleHandle(IntPtr ModuleName);
[DllImport("Kernel32.dll", SetLastError=true,
EntryPoint="GetModuleFileName")]
private static extern Int32 XP_GetModuleFileName(IntPtr hModule,
StringBuilder
ModuleName, Int32 cch);

public static IntPtr GetModuleHandle(IntPtr ModuleName)
{
try
{
return XP_GetModuleHandle(ModuleName);
}
catch (MissingMethodException)
{
return CE_GetModuleHandle(ModuleName);
}
}
public static Int32 GetModuleFileName(IntPtr hModule, StringBuilder
ModuleName, Int32 cch)
{
try
{
return XP_GetModuleFileName(hModule, ModuleName, cch);
}
catch (MissingMethodException)
{
return CE_GetModuleFileName(hModule, ModuleName, cch);
}
}


Tim Gerken
.NET Compact Framework Product Team
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Ralf Heitmann" <[email protected]>
| Subject: Missing GetEntryAssembly() again
| Date: Thu, 6 Nov 2003 16:51:31 +0100
|
| Hi there,
|
| I need to find out the "Entry Assembly" in CompactFramework.
| The full FX implements "Assembly.GetEntryAssembly()" for that case.
|
| I also found in this newsgroup a hint to implement this code
| (GetModuleFileName with P/Invoke).
|
| [DllImport("CoreDll.dll", SetLastError=true)]
| public static extern IntPtr GetModuleHandle(IntPtr ModuleName);
| [DllImport("CoreDll.dll", SetLastError=true)]
| public static extern Int32 GetModuleFileName(IntPtr hModule, StringBuilder
| ModuleName, Int32 cch);
|
| private static string GetEntryAssembly()
| {
| StringBuilder sb = null;
| IntPtr hModule = GetModuleHandle(IntPtr.Zero);
| if (IntPtr.Zero != hModule)
| {
| sb = new StringBuilder(255);
| if (0 == GetModuleFileName(hModule, sb, sb.Capacity))
| {
| sb = null;
| }
| }
| return sb.ToString();
| }
|
| But the important Drawback of this solution is that this code doesn't work
| when I start the Executable on WindowsXP.
| 1. Is there any other alternative to get the Entry Assembly?
| 2. How can get information about the running OS (WindowsXP) when I execute
| my CF code?
|
| Anyone done this?
|
| Thanks Ralf
|
|
|
 
Tim said:
Ralf, try using the following. Basically have a DllImport for both the CE
and XP versions of GetModuleHandle (and GetModuleFileName). Wrap them both
in a separate function that trys one, catches a MissingMethodException and
then tries the other.

[DllImport("CoreDll.dll", SetLastError=true,
EntryPoint="GetModuleHandle")]
private static extern IntPtr CE_GetModuleHandle(IntPtr ModuleName);
[DllImport("CoreDll.dll", SetLastError=true,
EntryPoint="GetModuleFileName")]
private static extern Int32 CE_GetModuleFileName(IntPtr hModule,
StringBuilder
ModuleName, Int32 cch);
[DllImport("Kernel32.dll", SetLastError=true,
EntryPoint="GetModuleHandle")]
private static extern IntPtr XP_GetModuleHandle(IntPtr ModuleName);
[DllImport("Kernel32.dll", SetLastError=true,
EntryPoint="GetModuleFileName")]
private static extern Int32 XP_GetModuleFileName(IntPtr hModule,
StringBuilder
ModuleName, Int32 cch);

public static IntPtr GetModuleHandle(IntPtr ModuleName)
{
try
{
return XP_GetModuleHandle(ModuleName);
}
catch (MissingMethodException)
{
return CE_GetModuleHandle(ModuleName);
}
}
public static Int32 GetModuleFileName(IntPtr hModule, StringBuilder
ModuleName, Int32 cch)
{
try
{
return XP_GetModuleFileName(hModule, ModuleName, cch);
}
catch (MissingMethodException)
{
return CE_GetModuleFileName(hModule, ModuleName, cch);
}
}


Tim Gerken
NET Compact Framework Product Team
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Ralf Heitmann" <[email protected]>
| Subject: Missing GetEntryAssembly() again
| Date: Thu, 6 Nov 2003 16:51:31 +0100
|
| Hi there,
|
| I need to find out the "Entry Assembly" in CompactFramework.
| The full FX implements "Assembly.GetEntryAssembly()" for that case.
|
| I also found in this newsgroup a hint to implement this code
| (GetModuleFileName with P/Invoke).
|
| [DllImport("CoreDll.dll", SetLastError=true)]
| public static extern IntPtr GetModuleHandle(IntPtr ModuleName);
| [DllImport("CoreDll.dll", SetLastError=true)]
| public static extern Int32 GetModuleFileName(IntPtr hModule, StringBuilder
| ModuleName, Int32 cch);
|
| private static string GetEntryAssembly()
| {
| StringBuilder sb = null;
| IntPtr hModule = GetModuleHandle(IntPtr.Zero);
| if (IntPtr.Zero != hModule)
| {
| sb = new StringBuilder(255);
| if (0 == GetModuleFileName(hModule, sb, sb.Capacity))
| {
| sb = null;
| }
| }
| return sb.ToString();
| }
|
| But the important Drawback of this solution is that this code doesn't work
| when I start the Executable on WindowsXP.
| 1. Is there any other alternative to get the Entry Assembly?
| 2. How can get information about the running OS (WindowsXP) when I execute
| my CF code?
|
| Anyone done this?
|
| Thanks Ralf
|
|
|
Thanks Tim, that was exactly I needed :-)))

Ralf
 
Back
Top