我看到这里讨论过非常相似的主题,但到目前为止,任何人都解决了我的问题。
我有这个任务:
我知道在64位进程上运行32位应用程序是不可能的。否则我通过COM IPC通信找到了解决方法。
所以有我的解决方案:
COM库的接口声明:
namespace Win32ConsoleAppWrapper
{
[GuidAttribute("5a6ab402-aa68-4d58-875c-fe26dea9c1cd"), ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IKDUCompessor
{
event ProcessStarted processStarted;
void RunKDUCompress(string comm);
}
}
实现界面的类:
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();
}
}
}
}
代码构建时没有错误,也没有警告。然后通过regAsm.exe注册为COM。
现在我正在尝试访问COM并调用方法:
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);
}
}
我遇到了问题:
我尝试了很多解决方案,但我没有成功...感谢您的帮助。
答案 0 :(得分:1)
不能从64位进程运行 32位。您不能在64位进程中使用 32位DLL或COM-Control,但可以使用Process.Start
启动任何所需的进程。
在你的情况下,听起来你甚至不必使用你正在描述的COM控件(这会导致更多问题,甚至)。只需在您的服务中执行以下操作(无论是32位还是64位),您就可以了:
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();
}