home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / wincom / dllcom / src / dllutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  1.9 KB  |  71 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "dllcom.h"
  20. #include <assert.h>
  21.  
  22. // Converts an OLE string (UNICODE) to an ANSI string. The caller
  23. // must use CoTaskMemFree to free the memory
  24. LPSTR
  25. AllocTaskAnsiString(LPOLESTR lpszString)
  26. {
  27.     LPSTR    lpszResult;
  28.     UINT    nBytes;
  29.  
  30.     if (lpszString == NULL)
  31.         return NULL;
  32.  
  33. #ifdef _WIN32
  34.     nBytes = (wcslen(lpszString) + 1) * 2;
  35. #else
  36.     nBytes = lstrlen(lpszString) + 1;  // Win 16 doesn't use any UNICODE
  37. #endif
  38.     lpszResult = (LPSTR)CoTaskMemAlloc(nBytes);
  39.  
  40.     if (lpszResult) {
  41. #ifdef _WIN32
  42.         WideCharToMultiByte(CP_ACP, 0, lpszString, -1, lpszResult, nBytes, NULL, NULL);
  43. #else
  44.         lstrcpy(lpszResult, lpszString);
  45. #endif
  46.     }
  47.  
  48.     return lpszResult;
  49. }
  50.  
  51. // The caller must free the returned string using CoTaskMemFree. The
  52. // returned string is an ANSI string
  53. LPSTR
  54. DLL_StringFromGUID(REFGUID rGuid)
  55. {
  56. #ifdef DLL_WIN32
  57.     OLECHAR    szGuidID[80];
  58.     int     iConvert = StringFromGUID2(rGuid, szGuidID, 80);
  59.  
  60.     assert(iConvert > 0);
  61.     return iConvert > 0 ? AllocTaskAnsiString(szGuidID) : NULL;
  62. #else
  63.     LPSTR    lpszGuidID;
  64.  
  65.     if (!SUCCEEDED(StringFromCLSID(rGuid, &lpszGuidID)))
  66.         return NULL;
  67.  
  68.     return lpszGuidID;
  69. #endif
  70. }
  71.