How to connect to windows phone 7 from a 64-bit application

别说谁变了你拦得住时间么 提交于 2019-12-20 06:30:28

问题


I have a 32-bit program (written in C++) that can connect to some different devices and as long as it is 32-bit everything works fine. However, now I need to build it as a 64-bit program but then I came across some problems with Windows Phone 7.

I found out that a dll (written in C#) that I rebuilt as 64-bit throws exception at this line:

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

The exception is:

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)).

(For example, if I try to run this example program it works in 32-bit but throws that exception in 64-bit at the same line)

When I searched for that CLSID in the registry I found a path to to "C:\Program Files (x86)\Common Files\Microsoft Shared\Phone Tools\CoreCon\11.0\Bin\ConMan2.dll" so I registered that dll using regsvr32 but I still get the same exception.

UPDATE:

Since I might need to create a workaround instead of finding a 64bit version of ConMan2.dll, I post a bit of my current dll here if anybody can show me a possible workaround so that it will work in both 32 and 64 bit.

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);
             ...
             ...           
        }
   }
}

回答1:


The COM server with CLSID = {349AB2E8-71B6-4069-AD9C-1170849DA64C} is implemented in C:\Program Files (x86)\Common Files\Microsoft Shared\Phone Tools\CoreCon\11.0\Bin\ConMan2.dll There’s no 64-bit version of that DLL. And you can’t use a 32-bit DLLs directly from a 64 bit process.

There’s a workaround. You can create another project, 32-bit EXE, that will call that 32-bit DLL however you want, and implement any IPC to interact with your main 64-bit application. For the specific IPC mechanism, if you only need to invoke a single relatively long task and wait for it to complete, command-like app + command-line arguments + exit code may me enough for you.

If you need to issue many calls, I’d choose WCF over named pipe transport. If you’ll choose this way, below is some sample code implementing that .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();
        }
    }
}


来源:https://stackoverflow.com/questions/33561504/how-to-connect-to-windows-phone-7-from-a-64-bit-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!