none
LDInline with nircmd.dll RRS feed

  • Question

  • I'm trying to use the NirCmd DLL with LDInline (ID: KQV552)

     and already tried a lot of thinkable variants to make it work for a simple command like standard beep 'stdbeep', 'cdrom open', etc. but cannot find out what's wrong there. While virt. compilation works well, the final LDInline.Call (in Sub RunCommand) fails with an error message 'An invocation target has caused an exception error.'  (transl. to engl.)

    This is one of my outputs in TW:

    using System;
    using System.Runtime.InteropServices;
    using Microsoft.SmallBasic.Library;

    public static class NirExt {
            [DllImport("nircmd.dll")]
            public static extern bool DoNirCmd(string NirCmdStr);

            public static void RunCmd(Primitive command) {
                    string strCmd = command;
                    NirExt.DoNirCmd(strCmd);
    } }
    --------------------------------------------------------------------------------
    * COMPILING ...
    result_Compile: SUCCESS

    * RUNNING 'NirExt.RunCmd("stdbeep")' ...
    LDInline.Call : Ein Aufrufziel hat einen Ausnahmefehler verursacht. (An invocation target has caused an exception error.)
    result_Run: FAILED

    Any idea??

    PS: the nircmd.dll (dated 14.Sept.2012)  is in my \Lib directory and in Program.Directory as well. The same commands work well from CmdLine(tool) with nircmd(c).exe command. So that cannot be the problem.

    PPS: For the NirCmd DLL call i used the usage sample from NirCmd DLL Samples (Visual Basic + C#)

     from the bottom of the side (in Form1.cs).

    Tuesday, February 23, 2016 10:06 PM
    Answerer

Answers

  • I got it to work in 32 bit (not 64) from C# - so it is a 32 bit dll - you need a 64 bitversion to run on a 64 bit machine since SB uses any cpu and will default to your architecture (64 bit a presume).
    Tuesday, February 23, 2016 10:58 PM
    Moderator

All replies

  • I first tried running with a try catch and got the error:

    An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

    using System;
    using System.Runtime.InteropServices;
    using Microsoft.SmallBasic.Library;
    
    public static class NirExt {
    	[DllImport("nircmd.dll")]
    	public static extern bool DoNirCmd(string NirCmdStr);
    
    	public static void RunCmd(Primitive command) {
    		try
    		{
    		 string strCmd = command;
    		 TextWindow.WriteLine(strCmd);
    		 NirExt.DoNirCmd(strCmd);
    		}
    		catch(Exception ex)
    		{
    		 TextWindow.WriteLine(ex.Message);
    		}
    } }

    I get the same error when I try to run the C# sample they provide to open CD draw?  I had to add the nircmd.dll to the test sample folder with the compiled exe, but I still get this error.

    So I would try to get it working in stanadlone C# first.  It is an unmaged dll, I can't see if it 32 or 64 bit.

    Tuesday, February 23, 2016 10:53 PM
    Moderator
  • I got it to work in 32 bit (not 64) from C# - so it is a 32 bit dll - you need a 64 bitversion to run on a 64 bit machine since SB uses any cpu and will default to your architecture (64 bit a presume).
    Tuesday, February 23, 2016 10:58 PM
    Moderator
  • Yes, that's it!

    I'm using W7/64 and the nircmd.dll is a 32 bit DLL.

    SigCheck.exe -q -a nircmd.dll    ->   MachineType:    32-bit

    file.exe nircmd.dll             ->   nircmd.dll; PE32 executable for MS Windows (DLL) (GUI) Intel 80386 32-bit

    The provided C# sample project (exe) works fine with <PlatformTarget>x86 and your upper demo code compiles well as an SB extension, also shows up in the SBIDE, but when run within SB gives an error  'Es wurde versucht, eine Datei mit einem falschen Format zu laden. (Ausnahme von HRESULT: 0x8007000B)'.

    Well, the next trouble on 64 bit. Things were a lot easier and unproblematic with 32 bit.

    Thanks a lot and i'll try to get a 64 bit Dll, or else simply use the CmdLine exe tool.

    Wednesday, February 24, 2016 10:17 PM
    Answerer
  • There are ways to run 32 bit from 64, but they rely on effectively running the 32 as an external process and communicating back through some other method like COM - its usually quite a bit of work.

    But the idea to call an 'out of process' command could be used, i.e. call the command line in some way in a batch or silent mode from C#.

    Thursday, February 25, 2016 10:17 AM
    Moderator
  • Thanks LitDev, just found some interesting links on a quick search for the keywords you specified, like KB article etc. and'll take a closer look at it on the weekend.
    Thursday, February 25, 2016 10:41 PM
    Answerer
  • For those who are interested, there is also a 64-bit version of NirCmd DLL existing now.

    Available commands are under Examples of what you can do with NirCmd - NirCmd Commands

    or NirCmd  (in the NirCmd.chm from the CmdLineTool download)

    But did'nt find the time to test it yet.

    Saturday, July 13, 2019 9:49 AM
    Answerer