如何从64位应用程序连接到windowsphone 7

本文关键字:windowsphone 连接 应用程序 64位 | 更新日期: 2023-09-27 18:20:44

我有一个32位程序(用C++编写),可以连接到一些不同的设备,只要是32位,一切都很好。然而,现在我需要将其构建为一个64位程序,但后来我在Windows Phone 7中遇到了一些问题。

我发现我重建为64位的dll(用C#编写)在以下行引发异常:

MultiTargetingConnectivity connectivity = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);

例外情况是:

An unhandled exception of type 'Microsoft.SmartDevice.Connectivity.DatastoreException' occurred in Microsoft.SmartDevice.Connectivity.dll
Additional information: Retrieving the COM class factory for component with CLSID {349AB2E8-71B6-4069-AD9C-1170849DA64C} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

(例如,如果我尝试运行这个示例程序,它在32位中工作,但在同一行的64位中抛出异常)

当我在注册表中搜索该CLSID时,我找到了"C:''Program Files(x86)''Common Files''Microsoft Shared''Phone Tools''CoreCon''11.0''Bin''ConMan2.dll"的路径,所以我使用regsvr32注册了该dll,但仍然得到了相同的异常。

更新:

由于我可能需要创建一个变通方法,而不是找到ConMan2.dll的64位版本,如果有人能向我展示一个可能的变通方法,我会在这里发布我当前dll的一部分,以便它在32位和64位都能工作。

namespace WP7DLL
{
    // Interface declaration.
    [Guid("11111111-1111-1111-1111-111111111111")]
    public interface IWP7DLL
    {
        int GetStatus();
    };
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("22222222-2222-2222-2222-222222222222")]
    public class WP7DLL : IWP7DLL
    {    
        public WP7DLL() { }
        public int GetStatus()
        {
             //Line that gives an exception in 64 bit
             MultiTargetingConnectivity connectivity = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);
             ...
             ...           
        }
   }
}

如何从64位应用程序连接到windowsphone 7

CLSID为{349AB2E8-71B6-4069-AD9C-1170849DA64C}的COM服务器在C:''Program Files(x86)''Common Files''Microsoft Shared''Phone Tools''CoreCon''11.0''Bin''ConMan2.dll中实现该DLL没有64位版本。而且您不能直接从64位进程使用32位DLL。

有一个变通办法。您可以创建另一个32位EXE项目,该项目将根据您的需要调用该32位DLL,并实现任何IPC以与您的主64位应用程序交互。对于特定的IPC机制,如果您只需要调用一个相对较长的任务并等待它完成,那么类似命令的应用程序+命令行参数+退出代码可能就足够了。

如果您需要发出许多调用,我会选择WCF而不是命名管道传输。如果您选择这种方式,下面是一些实现.EXE.的示例代码

/// <summary>The class from the shared assembly that defines WCF endpoint, and named events</summary>
public static class InteropShared
{
    // Host signals it's ready and listening. Replace the zero GUID with a new one
    public static readonly EventWaitHandle eventHostReady = new EventWaitHandle( false, EventResetMode.AutoReset, @"{00000000-0000-0000-0000-000000000000}" );
    // Client asks the host to quit. Replace the zero GUID with a new one
    public static readonly EventWaitHandle eventHostShouldStop = new EventWaitHandle( false, EventResetMode.AutoReset, @"{00000000-0000-0000-0000-000000000000}" );
    const string pipeBaseAddress = @"net.pipe://localhost";
    /// <summary>Pipe name</summary>
    // Replace the zero GUID with a new one.
    public const string pipeName = @"00000000-0000-0000-0000-000000000000";
    /// <summary>Base addresses for the hosted service.</summary>
    public static Uri baseAddress { get { return new Uri( pipeBaseAddress ); } }
    /// <summary>Complete address of the named pipe endpoint.</summary>
    public static Uri endpointAddress { get { return new Uri( pipeBaseAddress + '/' + pipeName ); } }
}
static class Program
{
    /// <summary>The main entry point for the application.</summary>
    [STAThread]
    static void Main()
    {
        // The class implementing iYourService interface that calls that 32-bit DLL
        YourService singletoneInstance = new YourService();
        using( ServiceHost host = new ServiceHost( singletoneInstance, InteropShared.baseAddress ) )
        {
            // iYourService = [ServiceContract]-marked interface from the shared assembly
            host.AddServiceEndpoint( typeof( iYourService ), new NetNamedPipeBinding(), InteropShared.pipeName );
            host.Open();
            InteropShared.eventHostReady.Set();
            // Wait for quit request
            InteropShared.eventHostShouldStop.WaitOne();
            host.Close();
        }
    }
}