It Won't Load My Assembly And It Won't Say Why

  • Thread starter William McIlroy
  • Start date
W

William McIlroy

I have used WinSigGen, the P/Invoke Interop Assistant, to rewrite some gnarly
Win32 code in C#. It is astonishing the decorations it devises. Anyway, the
class, below, will not load when it is called and I don't know why. The
message I get is BEGIN Could not load type 'SystemBackup.ERPU1' from assembly
'SystemBackup, Version=1.0.0.0, Culter=neutral, PublicKeyToken=null' END
That's what the exception handler says the platform is complaining about.
The "call" to the class, which is completely static, is ERPU1.mein(true)

ALSO, while you're puzzling over that, I need help assigning a pointer to
NewState so that it can be passed to AdjustTokenPrivileges(). There is
supposed to be a method in the System.IntPtr class called ToPointer(), but I
cannot get the compiler to accept my lame attempts to use it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SystemBackup
{
class ERPU1
{


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{

/// LUID->_LUID
public LUID Luid;

/// DWORD->unsigned int
public uint Attributes;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID
{

/// DWORD->unsigned int
public uint LowPart;

/// LONG->int
public int HighPart;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{

/// DWORD->unsigned int
public uint PrivilegeCount;

/// LUID_AND_ATTRIBUTES[1]

[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 1, ArraySubType =
System.Runtime.InteropServices.UnmanagedType.Struct)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
static LUID luid;
unsafe static TOKEN_PRIVILEGES NewState;
static System.IntPtr hToken;
/// TOKEN_ADJUST_PRIVILEGES -> (0x0020)
public const int TOKEN_ADJUST_PRIVILEGES = 32;
/// TOKEN_QUERY -> (0x0008)
public const int TOKEN_QUERY = 8;
/// SE_PRIVILEGE_ENABLED -> (0x00000002L)
public const int SE_PRIVILEGE_ENABLED = 2;
public const int FALSE = 0;
public const int TRUE = 1;
/// Return Type: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "GetCurrentProcess")]
public static extern System.IntPtr GetCurrentProcess();
/// Return Type: BOOL->int
///ProcessHandle: HANDLE->void*
///DesiredAccess: DWORD->unsigned int
///TokenHandle: PHANDLE->HANDLE*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "OpenProcessToken")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
OpenProcessToken([System.Runtime.InteropServices.InAttribute()] System.IntPtr
ProcessHandle, uint DesiredAccess, out System.IntPtr TokenHandle);
public static bool GetTokenForTheCurrentProcess()
{
System.IntPtr hCurrentProcessHandle = GetCurrentProcess();
return OpenProcessToken(hCurrentProcessHandle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken);
}

/// Return Type: BOOL->int
///lpSystemName: LPCSTR->CHAR*
///lpName: LPCSTR->CHAR*
///lpLuid: PLUID->_LUID*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "LookupPrivilegeValueA")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
LookupPrivilegeValueA([System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpSystemName, [System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpName, [System.Runtime.InteropServices.OutAttribute()] out LUID
lpLuid);
/// Return Type: BOOL->int
///hObject: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "CloseHandle")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
CloseHandle([System.Runtime.InteropServices.InAttribute()] System.IntPtr
hObject);
/// Return Type: BOOL->int
///TokenHandle: HANDLE->void*
///DisableAllPrivileges: BOOL->int
///NewState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///BufferLength: DWORD->unsigned int
///PreviousState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///ReturnLength: PDWORD->DWORD*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "AdjustTokenPrivileges")]
[return:
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
AdjustTokenPrivileges([System.Runtime.InteropServices.InAttribute()]
System.IntPtr TokenHandle,
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
bool DisableAllPrivileges, [System.Runtime.InteropServices.InAttribute()]
System.IntPtr NewState, uint BufferLength, System.IntPtr PreviousState,
System.IntPtr ReturnLength);
public static void mein(bool fEnable)
{
if (GetTokenForTheCurrentProcess())
{ }
else
{
MessageBox.Show("Unable to obtain token of current process.");
return;
}
if (LookupPrivilegeValueA("", "", out luid)) // first parameter
should be NULL
{ }
else
{
CloseHandle(hToken);
MessageBox.Show("Unable to execute LookupPrivilegeValue.");
return;
}
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes = (fEnable ?
(uint)SE_PRIVILEGE_ENABLED : (uint)0);
/*
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
0,
NULL,
NULL))
*/
unsafe
{
System.IntPtr pNewState = System.IntPtr.Zero;
System.IntPtr pNULL = System.IntPtr.Zero;
// pNewState.= (System.IntPtr)&NewState;
if (AdjustTokenPrivileges(hToken, false, pNewState, 0,
pNULL, pNULL))
{ }
else
{ }
}
}
}
}
 
F

Frans Bouma [C# MVP]

William said:
I have used WinSigGen, the P/Invoke Interop Assistant, to rewrite some gnarly
Win32 code in C#. It is astonishing the decorations it devises. Anyway, the
class, below, will not load when it is called and I don't know why. The
message I get is BEGIN Could not load type 'SystemBackup.ERPU1' from assembly
'SystemBackup, Version=1.0.0.0, Culter=neutral, PublicKeyToken=null' END
That's what the exception handler says the platform is complaining about.
The "call" to the class, which is completely static, is ERPU1.mein(true)

ALSO, while you're puzzling over that, I need help assigning a pointer to
NewState so that it can be passed to AdjustTokenPrivileges(). There is
supposed to be a method in the System.IntPtr class called ToPointer(), but I
cannot get the compiler to accept my lame attempts to use it.

If you have problems loading managed assembly dlls, you should look at
the fusion log, using fuslogvw.exe in the .NET SDK folder. It will tell
you where it looked and why it didn't load the assembly.

FB


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
W

William McIlroy

Well, that's somewhere to look. Two comments:

1) Nothing says "failure to load assembly" like the word FUSION.
2) No doubt there is a single-most-important-reason-why that could be put
into a fail-time message, but that would require a policy change.

I may be imagining things, but it could be that the little tool for writing
Win32 applications in C# may refer to Windows Operating System DLLs that
don't exist or that exist in different forms on different platforms (e.g.
32-bit versus 64-bit). And this might could be the problem. The program
without ERPU file works just dandy. So the problem is definitely CAUSED by
ERPU.
 
W

William McIlroy

I have run fuslogvw.exe (that's fusion log viewer from the DOT NET FRAMEWORK
SDK VERSION 2.0). It reports NOTHING unusual. I have isolated the code that
is causing the problem. We have a tiny main program whose CATCH clause NEVER
gains control. We have the ERPU class that is statically declared and a
method referenced in the main program. The program compiles cleanly. The
program fails reliably upon execution. The question is why?

===== MAIN PROGRAM =====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ERPUAPP
{
class Program
{
static void Main(string[] args)
{
try
{
ERPU.mein(false);
}
catch (Exception e)
{
Console.WriteLine("There was a type load exception.");
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
//Console.WriteLine(e.TypeName);
Console.WriteLine(e.Source);
return;
}
Console.WriteLine("Application loaded, executed, exited.");
return;
}
}
}





===== ERPU CLASS =====

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace ERPUAPP
{
class ERPU
{



[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{

/// LUID->_LUID
public LUID Luid;

/// DWORD->unsigned int
public uint Attributes;
}


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LUID
{

/// DWORD->unsigned int
public uint LowPart;

/// LONG->int
public int HighPart;
}


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{

/// DWORD->unsigned int
public uint PrivilegeCount;

/// LUID_AND_ATTRIBUTES[1]


[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 1, ArraySubType =
System.Runtime.InteropServices.UnmanagedType.Struct)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
static LUID luid;
unsafe static TOKEN_PRIVILEGES NewState;
static System.IntPtr hToken;
/// TOKEN_ADJUST_PRIVILEGES -> (0x0020)
public const int TOKEN_ADJUST_PRIVILEGES = 32;
/// TOKEN_QUERY -> (0x0008)
public const int TOKEN_QUERY = 8;
/// SE_PRIVILEGE_ENABLED -> (0x00000002L)
public const int SE_PRIVILEGE_ENABLED = 2;
public const int FALSE = 0;
public const int TRUE = 1;
/// Return Type: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "GetCurrentProcess")]
public static extern System.IntPtr GetCurrentProcess();
/// Return Type: BOOL->int
///ProcessHandle: HANDLE->void*
///DesiredAccess: DWORD->unsigned int
///TokenHandle: PHANDLE->HANDLE*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "OpenProcessToken")]
[return:

System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
OpenProcessToken([System.Runtime.InteropServices.InAttribute()]
System.IntPtr
ProcessHandle, uint DesiredAccess, out System.IntPtr TokenHandle);
public static bool GetTokenForTheCurrentProcess()
{
System.IntPtr hCurrentProcessHandle = GetCurrentProcess();
return OpenProcessToken(hCurrentProcessHandle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken);
}

/// Return Type: BOOL->int
///lpSystemName: LPCSTR->CHAR*
///lpName: LPCSTR->CHAR*
///lpLuid: PLUID->_LUID*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "LookupPrivilegeValueA")]
[return:

System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool

LookupPrivilegeValueA([System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpSystemName, [System.Runtime.InteropServices.InAttribute()]

[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
string lpName, [System.Runtime.InteropServices.OutAttribute()] out
LUID lpLuid);
/// Return Type: BOOL->int
///hObject: HANDLE->void*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",
EntryPoint = "CloseHandle")]
[return:

System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool
CloseHandle([System.Runtime.InteropServices.InAttribute()]
System.IntPtr
hObject);
/// Return Type: BOOL->int
///TokenHandle: HANDLE->void*
///DisableAllPrivileges: BOOL->int
///NewState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///BufferLength: DWORD->unsigned int
///PreviousState: PTOKEN_PRIVILEGES->_TOKEN_PRIVILEGES*
///ReturnLength: PDWORD->DWORD*
[System.Runtime.InteropServices.DllImportAttribute("advapi32.dll",
EntryPoint = "AdjustTokenPrivileges")]
[return:

System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool

AdjustTokenPrivileges([System.Runtime.InteropServices.InAttribute()]
System.IntPtr TokenHandle,

[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
bool DisableAllPrivileges, [System.Runtime.InteropServices.InAttribute()]
System.IntPtr NewState, uint BufferLength, System.IntPtr PreviousState,
System.IntPtr ReturnLength);
public static void mein(bool fEnable)
{
if (GetTokenForTheCurrentProcess())
{ }
else
{
Console.WriteLine("Unable to obtain token of current
process.");
return;
}
if (LookupPrivilegeValueA("", "", out luid)) // first
parameter should be NULL
{ }
else
{
CloseHandle(hToken);
Console.WriteLine("Unable to execute
LookupPrivilegeValue.");
return;
}
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes = (fEnable ?
(uint)SE_PRIVILEGE_ENABLED : (uint)0);
/*
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
0,
NULL,
NULL))
*/
unsafe
{
System.IntPtr pNewState = System.IntPtr.Zero;
System.IntPtr pNULL = System.IntPtr.Zero;
// pNewState.= (System.IntPtr)&NewState;
if (AdjustTokenPrivileges(hToken, false, pNewState, 0,
pNULL, pNULL))
{ }
else
{ }
}
}
}
}
 
B

Ben Voigt [C++ MVP]

William said:
I have run fuslogvw.exe (that's fusion log viewer from the DOT NET
FRAMEWORK SDK VERSION 2.0). It reports NOTHING unusual. I have

You should probably let us be the judge of that. If your assembly won't
load there should be something reported there, even if it is a successful
load then that is important information. Can you show the part of the
fusion log corresponding to one execution of your program?
 

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