Editing INI Files through COM. With some server software it would be handy for an administrator to be able to modify .ini files on the file via a web browser. This demonstration shows you how to do just that via COM API calls. This is a very basic implementation as is intended to get you started on exposing windows API calls via COM.
Regarding security. Some of the API calls would be very dangerous to expose on the internet because the effect the server which the COM is actually registered on. Thus you are opening up that machine to the internet. Also, some of the actions which the COM needs to perform with the API may require the use of a more priveledged user, other than the IUSER_MACHINENAME (anonymous internet user). The easiest way to get aroundt this problem would be to create another account on the machine, and give it the proper permissions. Then use MMC to add the COM to component services on Windows 2000 or Microsoft Transaction Server in Windows NT, and then, in the package that you create, allow the object to use the credentials of the new user you created.
COMFirst lets take a look at the COM portion. If you need more help understanding COM please refer to our resource on COM. We we need to expose the ini API declarations. Here is the VB code. Rem *************************************** Rem *** Windows API/Global Declarations *** Rem ***************************************
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Now we will have to expose the API global declaration in the COM environment through Public worker routines. Rem *************************************** Rem *** Public Worker Routines *** Rem ***************************************
Public Function ReadIni(sSectHeader As String, sVarName As String, sFileName As String) As String Dim strReturn As String strReturn = String(255, Chr(0)) ReadIni = Left$(strReturn, GetPrivateProfileString(sSectHeader, ByVal sVarName, "", strReturn, Len(strReturn), sFileName)) End Function
Public Function WriteIni(sSectHeader As String, sVarName As String, sValue As String, sFileName As String) As Integer WriteIni = WritePrivateProfileString(sSectHeader, sVarName, sValue, sFileName) End Function
Thats it for the object itself. Notice all we are doing is providing public functions to directly use the API functions. Once you have compiled the VB code and registered the DLL on your machine (Using MMC, or regsvr32), you are ready to move on to the ASP portion.
ASPThis is where we will implement the web interface for the above declared object. Review the code within iniEditor.index.asp. Some of the key lines are: set oIniEditor = server.createobject("iniEditor.ini")
This creates an instance of the iniEditor object so that we can use its public methods. msg = "Read Return Value: " & oIniEditor.ReadIni(cstr(sect),cstr(var),cstr(filename))
A call to the COM's ReadIni exposed method. We pass in the FORM parameters, and get the return value, which is the value associated with that particular variable of the ini file.
ConclusionWith this simple demo I have shown you how you can enable full INI editing from with your ASP page. I have also shown you how to expose server sided API's on the net.
Hope this helps and if you have any questions feel free and contact me any time. Robert Chartier
|