Topic
  • 5 replies
  • Latest Post - ‏2018-03-29T14:43:47Z by VinayMQ
MadhuraRai
MadhuraRai
2 Posts

Pinned topic Failed to load mqe.dll

‏2017-08-31T09:38:39Z | ibm-mq

I am trying to connect to MQ server using local user created in the system via C#. 

I am using Win serv 2008 R2, MQ client and Server versions are 8.0.0.6

To run the code as a local user , i have implemented impersonation. Now when i impersonate the user and try to connect, i get below connection error.

when run visual studio as different user (local user), it works very but not when i use impersonation.

*****************************

 

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'IBM.WMQ.MQQueueManager' threw an exception.
  Source=amqmdnet
  TypeName=IBM.WMQ.MQQueueManager
  StackTrace:
       at IBM.WMQ.MQQueueManager..ctor(String queueManagerName, String Channel, String ConnName)
       at MQExample.MQTest.ConnectMQ(String strQueueManagerName, String strQueueName, String strChannelInfo) in D:\Adidas\MQExample\MQExample\MQExample\MQTest.cs:line 83
       at MQExample.Form1.button1_Click(Object sender, EventArgs e) in D:\Adidas\MQExample\MQExample\MQExample\Form1.cs:line 33
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at MQExample.Program.Main() in D:\Adidas\MQExample\MQExample\MQExample\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146232828
       Message=Exception has been thrown by the target of an invocation.
       Source=mscorlib
       StackTrace:
            at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
            at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
            at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
            at System.Activator.CreateInstance(Type type, Boolean nonPublic)
            at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
            at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
            at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
            at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
            at IBM.WMQ.CommonServices.CreateCommonServices()
            at IBM.WMQ.CommonServices.TraceConstructor(String objectId, String sccsid)
            at IBM.WMQ.Nmqi.NmqiEnvironment..ctor(NmqiPropertyHandler nmqiPropertyHandler)
            at IBM.WMQ.Nmqi.NmqiFactory.GetInstance(NmqiPropertyHandler properties)
            at IBM.WMQ.MQQueueManager..cctor()
       InnerException: 
            HResult=-2146232828
            Message=Exception has been thrown by the target of an invocation.
            Source=amqmdnet
            StackTrace:
                 at IBM.WMQ.MQCommonServices..ctor()
            InnerException: 
                 HResult=-2146233088
                 Message=Failed to load mqe.dll from folder C:\Program Files\IBM\WebSphere MQ\bin\
                 Source=amqmdnet
                 StackTrace:
                      at IBM.WMQ.Nmqi.NativeManager.InitializeNativeApis(String mode)
                 InnerException: 

 

 

***************************

 

code in c#

 

 

**********************************

 

                using (new Tools.Impersonator("iibadmin", ".", "Test@1234"))
                {
                    string userafter = WindowsIdentity.GetCurrent().Name;

                    MQQueueManager queueManager = null;

                    queueManager = new MQQueueManager(QueueManagerName, channelName, connectionName);


                    int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE | MQC.MQOO_FAIL_IF_QUIESCING;
                    queue = queueManager.AccessQueue(QueueName, openOptions);

                    int depth = queue.CurrentDepth;

                    queueMessage = new MQMessage();

                    queueMessage.Format = MQC.MQFMT_STRING;
                    queueGetMessageOptions = new MQGetMessageOptions();
                    queue.Get(queueMessage, queueGetMessageOptions);
                    strReturn =
                    queueMessage.ReadString(queueMessage.MessageLength);

                    queue.Close();


                    queueManager.Disconnect();
                }

 

 

***************************

 

Impersonation code 

 

********************

public class Impersonator :
IDisposable
{
#region Public methods.
// ------------------------------------------------------------------
 
/// <summary>
/// Constructor. Starts the impersonation with the given credentials.
/// Please note that the account that instantiates the Impersonator class
/// needs to have the 'Act as part of operating system' privilege set.
/// </summary>
/// <param name="userName">The name of the user to act as.</param>
/// <param name="domainName">The domain name of the user to act as.</param>
/// <param name="password">The password of the user to act as.</param>
public Impersonator(
string userName,
string domainName,
string password )
{
ImpersonateValidUser( userName, domainName, password );
}
 
// ------------------------------------------------------------------
#endregion
 
#region IDisposable member.
// ------------------------------------------------------------------
 
public void Dispose()
{
UndoImpersonation();
}
 
// ------------------------------------------------------------------
#endregion
 
#region P/Invoke.
// ------------------------------------------------------------------
 
[DllImport("advapi32.dll", SetLastError=true)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
 
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern int DuplicateToken(
IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
 
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool RevertToSelf();
 
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern  bool CloseHandle(
IntPtr handle);
 
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
 
        private const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
        private const int LOGON32_PROVIDER_WinNT50 = 3;
 
 
 
        // ------------------------------------------------------------------
        #endregion
 
        #region Private member.
        // ------------------------------------------------------------------
 
        /// <summary>
        /// Does the actual impersonation.
        /// </summary>
        /// <param name="userName">The name of the user to act as.</param>
        /// <param name="domainName">The domain name of the user to act as.</param>
        /// <param name="password">The password of the user to act as.</param>
        private void ImpersonateValidUser(
string userName, 
string domain, 
string password )
{
WindowsIdentity tempWindowsIdentity = null;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
 
try
{
if ( RevertToSelf() )
{
if ( LogonUser(
userName, 
domain, 
password,
                        LOGON32_LOGON_INTERACTIVE,
                        LOGON32_PROVIDER_DEFAULT, 
ref token ) != 0 )
{
if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
{
tempWindowsIdentity = new WindowsIdentity( tokenDuplicate );
impersonationContext = tempWindowsIdentity.Impersonate();
}
else
{
throw new Win32Exception( Marshal.GetLastWin32Error() );
}
}
else
{
throw new Win32Exception( Marshal.GetLastWin32Error() );
}
}
else
{
throw new Win32Exception( Marshal.GetLastWin32Error() );
}
}
finally
{
if ( token!= IntPtr.Zero )
{
CloseHandle( token );
}
if ( tokenDuplicate!=IntPtr.Zero )
{
CloseHandle( tokenDuplicate );
}
}
}
 
/// <summary>
/// Reverts the impersonation.
/// </summary>
private void UndoImpersonation()
{
if ( impersonationContext!=null )
{
impersonationContext.Undo();
}
}
 
private WindowsImpersonationContext impersonationContext = null;
 
// ------------------------------------------------------------------
#endregion
}

 

*******************

 

Updated on 2017-08-31T09:41:59Z at 2017-08-31T09:41:59Z by MadhuraRai
  • Balashankar
    Balashankar
    2 Posts

    Re: Failed to load mqe.dll

    ‏2018-03-26T05:38:24Z  

    Does anyone having the solution for the above problem "Failed to load mqe.dll" for a impersonated local user ? I'm facing the same problem :(

  • VinayMQ
    VinayMQ
    23 Posts

    Re: Failed to load mqe.dll

    ‏2018-03-26T18:30:49Z  

    Does anyone having the solution for the above problem "Failed to load mqe.dll" for a impersonated local user ? I'm facing the same problem :(

    Please check whether impersonated user have the MQ environment setup ? 

    i.e. Environment variables:

    PATH
    LIB
    MQ_JAVA_DATA_PATH
    MQ_JAVA_INSTALL_PATH
    MQ_JAVA_LIB_PATH
    MQ_JRE_PATH


    Depends on the app (32/64 bit) then you need to point to the 32-bit MQ environment like wise for 64-bit.

    i.e. mqe.dll is found in:

    C:\Program Files\IBM\WebSphere MQ\bin\mqe.dll
    C:\Program Files\IBM\WebSphere MQ\bin64\mqe.dll

     

    you can also register all dotnet dlls to registry to just strikeout registry related issue

     

    dotnet register can found under bin directory . 

  • VinayMQ
    VinayMQ
    23 Posts

    Re: Failed to load mqe.dll

    ‏2018-03-26T18:32:40Z  
    • VinayMQ
    • ‏2018-03-26T18:30:49Z

    Please check whether impersonated user have the MQ environment setup ? 

    i.e. Environment variables:

    PATH
    LIB
    MQ_JAVA_DATA_PATH
    MQ_JAVA_INSTALL_PATH
    MQ_JAVA_LIB_PATH
    MQ_JRE_PATH


    Depends on the app (32/64 bit) then you need to point to the 32-bit MQ environment like wise for 64-bit.

    i.e. mqe.dll is found in:

    C:\Program Files\IBM\WebSphere MQ\bin\mqe.dll
    C:\Program Files\IBM\WebSphere MQ\bin64\mqe.dll

     

    you can also register all dotnet dlls to registry to just strikeout registry related issue

     

    dotnet register can found under bin directory . 

    https://www.ibm.com/developerworks/community/blogs/messaging/entry/How_to_run_a_MQ_NET_application_with_the_MQ_NET_assemblies_from_Non_primary_installation?lang=en 

  • Balashankar
    Balashankar
    2 Posts

    Re: Failed to load mqe.dll

    ‏2018-03-29T11:01:53Z  
    • VinayMQ
    • ‏2018-03-26T18:30:49Z

    Please check whether impersonated user have the MQ environment setup ? 

    i.e. Environment variables:

    PATH
    LIB
    MQ_JAVA_DATA_PATH
    MQ_JAVA_INSTALL_PATH
    MQ_JAVA_LIB_PATH
    MQ_JRE_PATH


    Depends on the app (32/64 bit) then you need to point to the 32-bit MQ environment like wise for 64-bit.

    i.e. mqe.dll is found in:

    C:\Program Files\IBM\WebSphere MQ\bin\mqe.dll
    C:\Program Files\IBM\WebSphere MQ\bin64\mqe.dll

     

    you can also register all dotnet dlls to registry to just strikeout registry related issue

     

    dotnet register can found under bin directory . 

    I tried all the points above, but still didn't works.

    I'm using windows 10 profession 64 bit with MQ client 9.0.5.

    I can able to connect unless I logon as the impersonated user.

    Any thoughts, why I can't able to connect under other user(both the accounts are administrators) ?

    Updated on 2018-03-29T11:07:25Z at 2018-03-29T11:07:25Z by Balashankar
  • VinayMQ
    VinayMQ
    23 Posts

    Re: Failed to load mqe.dll

    ‏2018-03-29T14:43:47Z  

    I tried all the points above, but still didn't works.

    I'm using windows 10 profession 64 bit with MQ client 9.0.5.

    I can able to connect unless I logon as the impersonated user.

    Any thoughts, why I can't able to connect under other user(both the accounts are administrators) ?

    Do you have any related permission errors or only dll errors ? make sure all permissions are in place better to keep group permissions for all users even impersonated user . 

    Updated on 2018-03-29T17:19:44Z at 2018-03-29T17:19:44Z by VinayMQ