注册代码如下:
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//
//=================================================// //If returns Zero, DLL successfully registered... // -2 means DLL can not be loaded.. // -3 means DLL Entry point can not be found.. // -4 means Could not register the file... // DLL Registration failed.. //================================================// int RegisterComponent(char *absPath) { HINSTANCE hDLL = LoadLibrary(absPath); if(hDLL == NULL) { //-2 means DLL can not be loaded.. return -2; } typedef HRESULT (CALLBACK *HCRET)(void); HCRET lpfnDllRegisterServer; lpfnDllRegisterServer = (HCRET)GetProcAddress(hDLL, "DllRegisterServer"); if(lpfnDllRegisterServer == NULL) { //-3 means DLL Entry point can not be found.. return -3; } //Call the function by function pointer.. if(FAILED((*lpfnDllRegisterServer)())) { //-4 means Could not register the file... //DLL Registration failed.. return -4; } return 0; } // |
测试代码:
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//
int nVal = RegisterComponent("C:\KvDateTime.OCX"); if(nVal == 0) { AfxMessageBox("Component Successfully Registered..."); } else if(nVal == -2) { AfxMessageBox("DLL can not be loaded.. Reason could " "be path is incorrect or.. Component is corrupt"); } else if(nVal == -3) { AfxMessageBox("DLL Entrypoint for function " "DLLRegisterServer could not be found.."); } else if(nVal == -4) { AfxMessageBox("DLL Registration Failed.."); } else { AfxMessageBox("Unknown error in registering the file.."); } // |