extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
//Get an instance of this DLL for Self-Registration
g_hinstDll = hInstance;
TRACE0("Kkk.DLL Initializing!\n");
// Extension DLL one-time initialization
AfxInitExtensionModule(KkkDLL, hInstance);
// Insert this DLL into the resource chain
new CDynLinkLibrary(KkkDLL);
pDynLinkLibrary = new CDynLinkLibrary(KkkDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("Kkk.DLL Terminating!\n");
// shut down the sample application
TheApplication->TerminateApplication();
delete TheApplication;
// Terminate the library before destructors are called
delete pDynLinkLibrary;
AfxTermExtensionModule(KkkDLL);
}
return 1; // ok
}
// --------------------------------
// connections to SolidWorks
// --------------------------------
// this is the function SolidWorks calls to connect
// to the add-in application
long InitUserDLL3(LPSLDWORKS pSldWorks)
{
// this function should be called once per session
if ( TheApplication != NULL )
{
ASSERT ( FALSE );
return 0;
}
// start the application
TheApplication = new CKkkApp(pSldWorks);
if (!TheApplication->StartApplication())
return 0;
STDAPI DllRegisterServer(void) {
HRESULT hr = S_OK;
//Look up the server's file name
char szFileName[MAX_PATH];
GetModuleFileNameA(g_hinstDll, szFileName, MAX_PATH);
//register entries from table
int nEntries = sizeof(g_RegTable)/sizeof(*g_RegTable);
for (int i = 0; SUCCEEDED(hr) [$& i < nEntries] i++) {
const char *pszKeyName = g_RegTable[0];
const char *pszValueName = g_RegTable[1];
const char *pszValue = g_RegTable[2];
//map rogue value to module file name
if (pszValue == (const char*)-1)
pszValue = szFileName;
HKEY hkey;
//create the key
long err = ERROR_SUCCESS;
if (i <= 1) { // the first keys are in HKEY_LOCAL_MACHINE
err = RegCreateKeyA(HKEY_LOCAL_MACHINE, pszKeyName, [$hkey)]
} else { //The rest go into HKEY_CLASSES_ROOT
err = RegCreateKeyA(HKEY_CLASSES_ROOT, pszKeyName, [$hkey)]
}
if (err == ERROR_SUCCESS) {
//set the value
err = RegSetValueExA(hkey, pszValueName, 0, REG_SZ, (const BYTE*)pszValue, (strlen(pszValue)+1));
RegCloseKey(hkey);
}
if (err != ERROR_SUCCESS) {
//if cannot add key or value, back out and fail
DllUnregisterServer();
hr = SELFREG_E_CLASS;
}
}
return hr;
}
//Unregister the DLL
STDAPI DllUnregisterServer(void) {
HRESULT hr = S_OK;
int nEntries = sizeof(g_RegTable)/sizeof(*g_RegTable);
for (int i = nEntries - 1; i >= 0; i--) {
if (i <= 1) { //The last keys are in HKEY_LOCAL_MACHINE
err = RegDeleteKeyA(HKEY_LOCAL_MACHINE, pszKeyName);
} else { //Remove the rest from HKEY_CLASSES_ROOT
err = RegDeleteKeyA(HKEY_CLASSES_ROOT, pszKeyName);
}