(FFOS Gecko)

1. Using a service in C++

nsCOMPtr<nsIServiceManager> servManager;
nsresult rv = NS_GetServiceManager(getter_AddRefs(servManager));
if (NS_FAILED(rv)) { // get ServiceManager error
  return -1;
}

// get real Service
nsCOMPtr<nsICustomService> customService;
rv = servMan->GetServiceByContractID("@mozilla.org/customservice",
                                     NS_GET_IID(nsICustomService),
                                     getter_AddRefs(customService));
if (NS_FAILED(rv)) { // get CustomService error
  return -1;
}

// call a method of CustomService
customService->callMethod(/*some parameters*/);

2. Using a service in Javascript(XPConnect)

customService = Components.classes["@mozilla.org/customservice;1"]
                 .getService();
customService = customService.QueryInterface(Components.interfaces.nsICustomService);
customService.callMethod();
原文地址:https://www.cnblogs.com/code-4-fun/p/4661738.html