Click here to Skip to main content
13,149,756 members (81,215 online)
Click here to Skip to main content

Stats

32.4K views
277 downloads
9 bookmarked
Posted 16 Aug 2010

String Handling in XPCOM

Using different types of strings in XPCOM.
// xpcom_client.cpp : Defines the entry point for the console application.
//

	#include "stdafx.h"
	#include "nsCOMPtr.h"
	#include "nsXPCOMStrings.h"
	#include "nsXPCOM.h"
	#include "../sample_xpcom/ISample.h"
	#include "../sample_xpcom/Sample.h"
	#include "nsServiceManagerUtils.h"
	#include "nsStringAPI.h"


	int _tmain(int argc, _TCHAR* argv[])
	{
		nsresult rv;

		nsCOMPtr<nsIServiceManager> servMan;

		// Get Service manager
		rv = NS_GetServiceManager(getter_AddRefs(servMan));
		if (NS_FAILED(rv))
		{
			printf("ERROR: XPCOM error [%x].\n", rv);
			return -1;
		}

		// Get the Component object;
		nsCOMPtr<ISample> iSample;
		rv = servMan->GetServiceByContractID(SAMPLE_COMPONENT_CONTRACTID, NS_GET_IID(ISample), getter_AddRefs(iSample));	
		
		// Or you can get like this
		//rv = servMan->GetService(NS_GET_IID(CSample), NS_GET_IID(ISample), getter_AddRefs(iSample));
		
		
		if ( NS_FAILED(rv) )
		{
			NS_ShutdownXPCOM(nsnull);
			return -1;
		}

		int nFirstVal, nSecondVal, nResult;
		nFirstVal= 15; 
		nSecondVal = 10;
		iSample->Add(nFirstVal, nSecondVal, &nResult);

		_tprintf(_T("\nThe Result is : %d\n"), nResult);


		//---------------------------------------------------------------
		//----------------------- Using String types --------------------
		//---------------------------------------------------------------



		// IDL type		C++ Type		Purpose
		// string	    char*			Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries.

		char chMainStr[256] = {'\0'};
		char *chRevStr;
		strcpy(chMainStr, "Test string");
		iSample->ReverseString(chMainStr, (char**)&chRevStr);
		printf("\nThe Main String: %s  The Reverted String : %s.\n", chMainStr, chRevStr);


		//	IDL type		C++ Type		Purpose
		//	wstring			PRUnichar*		Raw character pointer to UTF-16 string, no string classes used.

		wchar_t wchMainStr[256] = {'\0'};
		wchar_t *wchRevStr;	

		wcscpy(wchMainStr, L"Test wstring");
		iSample->ReverseUnichar(wchMainStr, &wchRevStr);
		wprintf(L"\nThe Main String: %s  The Reverted String : %s.\n", wchMainStr, wchRevStr);


		// IDL type			C++ Type		Purpose
		// AString			nsAString		UTF-16 string.
		nsString nsStringRev;
		nsString nsStringMain(L"Test AString");
		wcscpy(wchMainStr, nsStringMain.get());
		
		iSample->ReverseAString(nsStringMain, nsStringRev);
		
		wcscpy(wchRevStr, nsStringRev.get());

		wprintf(L"\nThe Main String: %s  The Reverted String : %s.\n", wchMainStr, wchRevStr);


		// IDL type			C++ Type		Purpose
		// ACString			nsACString		8-bit string. All bits are preserved across XPConnect boundaries.
		nsCString nsCStringRev;
		nsCString nsCStringMain("Test ACString");
		strcpy(chMainStr, nsCStringMain.get());
		iSample->ReverseACString(nsCStringMain, nsCStringRev);
		strcpy(chRevStr, nsCStringRev.get());
		
		printf("\nThe Main String: %s  The Reverted String : %s.\n", chMainStr, chRevStr);

		// Shutdown Service manager 
		NS_ShutdownXPCOM(nsnull);
		
		return 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)

Share

About the Author

Md. Mazharul Islam Khan
Software Developer (Senior) Eyeball Networks Inc, Dhaka
Bangladesh Bangladesh
I strongly believe, a person's strongest point is to know his/her weak point. I try to be simple.

I have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).


Watch out for my other CodeProject Articles.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 16 Aug 2010
Article Copyright 2010 by Md. Mazharul Islam Khan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid