HOWTO: Open a SourceSafe Database with OLE Automation in C++Last reviewed: June 12, 1997Article ID: Q169928 |
The information in this article applies to:
SUMMARYThis article contains sample C++ code along with an explanation of how to get a pointer to a SourceSafe database object and execute the Open method to start a session with a SourceSafe database.
MORE INFORMATIONVisual SourceSafe must be registered before you can use its OLE Automation Model. The following CLSID should be found in HKEY_CLASSES_ROOT/CLSID: {783CD4E4-9D54-11CF-B8EE-00608CC9A71F}. Registration occurs by default when you install the SourceSafe Client. To work with the objects in the SourceSafe OLE Model, first you must have access to a VSSDatabase object, and call its Open method.
Use the MultiByteToWide function to convert an LPSTR to OLECHAR* as shown in the following code fragment:
if((x=MultiByteToWideChar(CP_ACP,0,path,-1,svalue,0)) != 1) { svalue = (OLECHAR*)malloc( x*sizeof(wchar_t) ); if(MultiByteToWideChar(CP_ACP, 0, path, -1, svalue, x ) == 0) MessageBox( NULL, "Error in Conversion", "Multibytetowide", MB_OK ); } else svalue = L"";MultiByteToWide counts the characters and determines the size of the buffer needed to hold the BSTR. The appropriate size buffer is then allocated and the conversion is done. In the example above svalue is an OLECHAR*, and path is an LPSTR.
Sample CodeThe following sample code demonstrates how to open a SourceSafe database as described above:
// Be sure to link with the following libraries: // user32.lib uuid.lib oleaut32.lib ole32.lib #define INITGUID #include <windows.h> #include <ocidl.h> #include "ssauto.h" int main () { CLSID clsid; IClassFactory *pClf; IVSSDatabase *pVdb; BSTR bstrPath = SysAllocString(L"c:\\VSSclient\\srcsafe.ini"); BSTR bstrUName = SysAllocString(L"guest"); BSTR bstrUPass = SysAllocString(L""); CoInitialize(0); if(S_OK == CLSIDFromProgID(L"SourceSafe", &clsid )) { if(S_OK == CoGetClassObject( clsid, CLSCTX_ALL, NULL, IID_IClassFactory, (void**)&pClf )) { if(S_OK == pClf->CreateInstance( NULL, IID_IVSSDatabase, (void **) &pVdb )) { if(S_OK == pVdb->Open(bstrPath, bstrUName, bstrUPass)) { //Database Successfully Opened! //Add code here to use the open database. } pVdb->Release(); } pClf->Release(); } } CoUninitialize(); SysFreeString(bstrPath); SysFreeString(bstrUName); SysFreeString(bstrUPass); return 0; } REFERENCESTITLE: SysAllocString URL: mk:@ivt:pdobj/native/sdk/ole/auto/src/chap7_27.htmTITLE: SysFreeString URL: mk:@ivt:pdobj/native/sdk/ole/auto/src/chap7_30.htmTITLE: CLSIDFromProgID URL: mk:@ivt:pdobj/native/activex/src/api1_1.htmTITLE: IClassFactory URL: mk:@ivt:pdobj/native/activex/src/if_a2e_68.htmTITLE: CoGetClassObject URL: mk:@ivt:pdobj/native/activex/src/api1_19.htmTITLE: Visual SourceSafe OLE Automation URL: http://www.microsoft.com/SSAFE/download/oleauto.docThe header file ssauto.h can be downloaded from http://www.microsoft.com/SSAFE/download/ssauto.h. |
Keywords : ssusage
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |