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

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

我发现我重建为64位的dll(用C#编写)会在此行抛出exception:

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位引发该exception)

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

更新:

由于我可能需要创建一个解决方法,而不是找到64位版本的ConMan2.dll,如果有人能告诉我一个可能的解决方法,那么我会在这里发布一些我当前的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); ... ... } } } 

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

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

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

 /// The class from the shared assembly that defines WCF endpoint, and named events 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"; /// Pipe name // Replace the zero GUID with a new one. public const string pipeName = @"00000000-0000-0000-0000-000000000000"; /// Base addresses for the hosted service. public static Uri baseAddress { get { return new Uri( pipeBaseAddress ); } } /// Complete address of the named pipe endpoint. public static Uri endpointAddress { get { return new Uri( pipeBaseAddress + '/' + pipeName ); } } } static class Program { /// The main entry point for the application. [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(); } } }