Editing INI Files through COM by Robert Chartier

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
September 17, 2000
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.


COM

First 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.


ASP

This 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.


Conclusion

With 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

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials