简体   繁体   中英

32bit console application cannot be started in 64bit process written in c#

I see that quite similar topic has been discussed here but anyone has solved my problem so far.

I have this assignment:

  1. I have .exe (32bit) utility to run with commands
  2. this utility will be started with windows service on 64bit platform

I know that this is no possibility to run 32bit apps on 64bit process. Otherwise I found workaround by COM IPC communication.

So there is my solution:

Interface declaration of COM library:

namespace Win32ConsoleAppWrapper
{
  [GuidAttribute("5a6ab402-aa68-4d58-875c-fe26dea9c1cd"), ComVisible(true),
  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  interface IKDUCompessor
  {
    event ProcessStarted processStarted;
    void RunKDUCompress(string comm);
  }
}

Class implementing the interface:

namespace Win32ConsoleAppWrapper
{

  [GuidAttribute("a1f4eb1a-b276-4272-90e0-0eb26e4273e0"), ComVisible(true),
  ClassInterface(ClassInterfaceType.None)]
  public class KDUCompessor : IKDUCompessor
  {
    public static readonly string KDU_COMPRESS_LIBRARY_NAME = "kdu_compress.exe";

    public event ProcessStarted processStarted;

    public KDUCompessor() { }

    public void RunKDUCompress(string comm)
    {
      if(!File.Exists(KDU_COMPRESS_LIBRARY_NAME))
      {
        throw new FileNotFoundException(String.Format("File {0} could not be found. Please check the bin directory.", KDU_COMPRESS_LIBRARY_NAME));
      }

      ProcessStartInfo info = new ProcessStartInfo();
      info.CreateNoWindow = false;
      info.UseShellExecute = true;
      info.FileName = String.Concat(KDU_COMPRESS_LIBRARY_NAME);
      info.WindowStyle = ProcessWindowStyle.Hidden;
      info.Arguments = comm;

      // Start the process with the info we specified.
      // Call WaitForExit and then the using statement will close.
      using(Process exeProcess = Process.Start(info))
      {
        exeProcess.WaitForExit();
      }      
    }
  }
}

The code is build with no error and no warnings. Then by regAsm.exe registered as COM.

Now I am trying to access to COM and call the method:

public class MyClass
{

#region COM Interface and class declaration

[
  ComImport(),
  Guid("5a6ab402-aa68-4d58-875c-fe26dea9c1cd"),
  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface IKDUCompessor
{
  [PreserveSig]
  void RunKDUCompress(string comm);   

}

[
  ComImport,
  Guid("a1f4eb1a-b276-4272-90e0-0eb26e4273e0")
]
public class KDUCompessor { }

#endregion

protected void CallCOM(_command)
{
  if (IsCommandValid(_command))
  {
    // instance
    Type type = Type.GetTypeFromCLSID(new Guid("a1f4eb1a-b276-4272-90e0-0eb26e4273e0"));
    object o = Activator.CreateInstance(type);
    IKDUCompessor t = (IKDUCompessor)o;
    t.RunKDUCompress(_command);

  }
}

And I got a problems:

  1. executing ends with exception: System.Runtime.InteropServices.COMException was unhandled, class is not registered HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)), when assembly is registered by regasm.exe with no error
  2. I cannot add COM reference to project in VS via add reference wizard, it ends with error dialog window: "A reference to 'assemblyName' could not be added. The ActiveX type library 'libary path' was exported from a .net assembly and cannot be added as a reference. Add a reference to the .net assembly instead."

I tried a lot of solutions but I was unsuccessful ... thanks for help.

It is not true that you can't run a 32-bit from a 64-bit process. You can not use a 32-bit DLL or COM-Control in a 64-bit process, but you can use Process.Start to start any process you want.

In your case it sounds like you don't even have to use the COM control you're describing (this causes more problems, even). Just do the following within your service (no matter whether 32- or 64-bit) and you're fine:

if(!File.Exists(KDU_COMPRESS_LIBRARY_NAME))
{
    throw new FileNotFoundException(String.Format("File {0} could not be found. Please check the bin directory.", KDU_COMPRESS_LIBRARY_NAME));
}

ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = false;
info.UseShellExecute = true;
info.FileName = String.Concat(KDU_COMPRESS_LIBRARY_NAME);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = comm;

// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using(Process exeProcess = Process.Start(info))
{
    exeProcess.WaitForExit();
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM