Click here to Skip to main content
15,891,828 members
Articles / Mobile Apps / Windows Mobile

Bringing DCOM remoting functionality to Windows CE and .NET CF2.0

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
17 Apr 2006CPOL14 min read 97.1K   513   40  
This article shows how to use DCOM on Windows CE 5.0. We will add full DCOM rich error information, and implement a DCOM interface between a Windows XP .NET 2.0 client and Windows CE DCOM server. With this code, it is possible to code .NET remoting alike functionality through DCOM interop.
// 
// Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
// 
// Use of this source code is subject to the terms of the Microsoft end-user 
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT. 
// If you did not accept the terms of the EULA, you are not authorized to use 
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your 
// install media. 
// 
#include <windows.h> 
#include <stdio.h> 
#include <objbase.h> 


typedef HRESULT (CALLBACK *LPFNINSTFUNC1)(BOOL, LPCWSTR); 


#define MINARGS    1     // Min. # of arguments 


// 
//  DebugOut 
// 
//  printf type function for outputting debug information 
// 
void DebugOut( LPTSTR lpFormat, ... ) 
{ 
    TCHAR   wszOut[512]; 
    va_list args; 


    va_start( args, lpFormat ); 


    _vstprintf( wszOut, lpFormat, args ); 


#ifdef UNDER_CE 
    OutputDebugString( wszOut ); 
#else 
    printf( TEXT("%s\n"), wszOut ); 
#endif 
    wprintf( TEXT("%s\n"), wszOut ); 
    va_end( args ); 
} 


void PrintUsage (TCHAR *wszprogName) 
{ 
    DebugOut (_T("Usage: %s [/u] [/n] [/i[:cmdline]] dllname\n"), wszprogName); 
    DebugOut (_T("/u-\tUnregister server\n")); 
    DebugOut (_T("/s-\tSilent; Display no messages\n")); 
    DebugOut (_T("/i-\tCall DllInstall passing it an optional [cmdline]; when used with /u calls dll uninstall\n")); 
    DebugOut (_T("/n-\tdo not call DllRegisterServer; this option must be used with /i\n")); 
} 

// Make sure following is present in the project
// /subsystem:$(CESubsystem) -> /subsystem:CONSOLE
// /entry=mainWCRTStartup for wmain
// library : corelibc.lib

void _tmain(int argc, TCHAR **argv) 
{ 
    HRESULT hr; 
    TCHAR *name = argv[1], *cmdline = NULL; 
    unsigned char fUnreg = FALSE, fInst = FALSE, fNoreg = FALSE, fSilent = FALSE; 


    CoInitializeEx(NULL, COINIT_MULTITHREADED); 

	int index;
    for (index=1; argc >= (index+MINARGS); name=argv[++index]) 
    { 
       if (_tcsnicmp (name, _T("/i"), 2) == 0) { 


         if (_tcslen (name) > 3) 
            cmdline = &(name[3]); 


         fInst = TRUE; 
       } 


       else if (_tcsicmp (name, _T("/u")) == 0) 
         fUnreg = TRUE; 


       else if (_tcsicmp (name, _T("/n")) == 0) 
         fNoreg = TRUE; 


       else if (_tcsicmp (name, _T("/s")) == 0) 
         fSilent = TRUE; 


       else if (_tcsicmp (name, _T("/?")) == 0) 
       { 
         PrintUsage (argv[0]); 
         exit(1); 
       } 


       else 
       { 
         break; 
       } 

    } 

    if ((argc < (index+MINARGS)) || (fNoreg && !fInst)) { 
        PrintUsage (argv[0]); 
        exit(1); 
    } 

    HINSTANCE hLL = LoadLibrary (name); 
    if (! hLL) { 
        DebugOut (_T("Could not load %s\n"), name); 
        exit(1); 
    } 


    if (!fNoreg) 
    { 
        FARPROC fp = GetProcAddress (hLL, 
#ifdef UNDER_CE 
                        (fUnreg ? L"DllUnregisterServer" : L"DllRegisterServer")); 
#else 
                        (fUnreg ? "DllUnregisterServer" : "DllRegisterServer")); 
#endif 
        if (! fp) { 
            DebugOut (_T("Registration functions are not exported from %s\n"), name); 
            exit(1); 
        } 

        hr = fp(); 

        if (FAILED(hr)) { 
            DebugOut(_T("Registration Operation failed hr = 0x%08x\n"), hr); 
            exit(1); 
        } 
     } 


    if (fInst) 
    { 
        if (cmdline && (*cmdline == _T('"'))) 
        { 
            WORD wLen = _tcslen (cmdline); 
            if (cmdline[wLen-1] == _T('"')) 
                cmdline[wLen-1] = _T('\0'); 


            cmdline++; 
         } 


#ifdef UNDER_CE 
        LPFNINSTFUNC1 fpInst = (LPFNINSTFUNC1) GetProcAddress (hLL, L"DllInstall"); 
#else 
        LPFNINSTFUNC1 fpInst = (LPFNINSTFUNC1) GetProcAddress (hLL, "DllInstall"); 
#endif 
        if (! fpInst) { 
            DebugOut (_T("DllInstall is not exported from %s\n"), name); 
            exit(1); 
        } 


#ifdef UNICODE 
        hr = fpInst( (fUnreg ? FALSE : TRUE), cmdline); 
#else 
        hr = fpInst( (fUnreg ? FALSE : TRUE), uxdup (cmdline)); 
#endif 


        if (FAILED(hr)) { 
            DebugOut (_T("DllInstall failed hr = 0x%08x"), hr); 
            exit(1); 
        } 
    } 


    if (!fSilent) 
    { 
        if (!fNoreg) 
        { 
            DebugOut (_T("%s"), (fUnreg ? _T("DllUnregisterServer") : _T("DllRegisterServer"))); 
            if (fInst) DebugOut (_T(" and ")); 
        } 

        if (fInst) DebugOut (_T("DllInstall")); 

        DebugOut (_T(" in %s succeeded.\n"), name); 
     } 

    CoUninitialize(); 

    exit(0); 
} 


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions