Monday, 13 January 2014

Read, Write, Search and Delete Registry Keys using Mapbasic and Windows API

In this post I will explain briefly how to access the windows registry using the Window API within Mapbasic. The example code below demontrates how to read a registry key, write a registry key and delete a registry key.

It is expected that you have a good understanding of developing within Mapbasic. This code could be used for setting up a licence key or storing information about toolbar position. I hope this is useful to someone.

Warning: If you know nothing about the window registry it is recommended that you read up before implementing this code. I accept no responsibility if you destroy your registry.

To download a copy of the source code click here RegistryExample.mb

The windows dll used to access the registry is advapi32.dll

The methods used are;

RegOpenKeyA
RegCloseKey
RegCreateKeyA
RegDeleteValueA
RegQueryValueExA
RegSetValueExA

This is the example test harness dialog:








The Create button will generate a new registry key in HKEY_CURRENT_USER\Software\Mapbasic\ExampleKey.

The code in the RegistryExample.mb example that generates the key uses method RegCreateKeyA.
 
Dim iResult as Integer
Dim hKey as Integer

iResult = RegCreateKey(hKeyRoot,strKeyPath,hKey)

iResult will return 0 if successful, otherwise a number greater than 0 will be returned. hKey contains the integer reference to the newly generated key (hKey value is used to set, query and delete keys later in the code).


Note: To access the registry editor type regedit in the start search

To set the registry key value, type a value in the edit text box and click Set.

iResult = RegSetValueEx(hKey,strKeyName, 0, iKeyType, strData, Len(strData))


To query the registry key value click Get. The edit text box will be populated with the value.


iResult = RegQueryValueEx(hKey,strKeyName,0,iReturnType,strKeyValue,iReturnDataBufSize)



To delete the example key click Delete.

iResult =   RegDeleteValue(hKey,strKeyName)



Cancel will end the mbx.

TRAPS:
I found that when you query using RegQueryValueEx you must pad your return string and parse in a large buffer size, otherwise an erroneous result will be returned indicating that the response is bigger than the buffer allocated. Not normally something you have to worry about in MapBasic but we are calling function in an unmanaged fashion. 

Dim iReturnDataBufSize As Integer
iReturnDataBufSize = 2147483646 'Max Integer Size
Dim strKeyValue As String
strKeyValue = String$(255," ")

I hope the code is fairly self explanatory and if anyone has any questions don't hesitate to comment.

Cheers

James


2 comments:

  1. Thanks for sharing!!!
    I've used your code when I had to change my license server machine for hundreds of users.
    I ran the program from every workspace on the GIS drive
    (Beginning of Sub Main):
    Dim strRetVal as String
    Dim strError as String
    Dim iKeyType as Integer
    Dim strData as String
    Dim iRetVal as Integer
    strData="SK50221"

    If GetRegistryKeyValue(HKEY_LOCAL_MACHINE,"SOFTWARE\Wow6432Node\MapInfo\MapInfo\Professional\1150", "LicenseServerName",iKeyType,strRetVal,strError) then
    'Print strRetVal+" :Licensserver - 64 bit"
    If strRetVal="SK40728"
    Then
    If Not SetRegistryKeyValue(HKEY_LOCAL_MACHINE,"SOFTWARE\Wow6432Node\MapInfo\MapInfo\Professional\1150","LicenseServerName",REG_SZ,strData,iRetVal,strError) Then
    Note strError
    Else
    Print "Licensserver er opdateret til "+strData
    End If
    End If
    ElseIf GetRegistryKeyValue(HKEY_LOCAL_MACHINE,"SOFTWARE\MapInfo\MapInfo\Professional\1150", "LicenseServerName",iKeyType,strRetVal,strError) then
    'Print strRetVal+" :Licensserver - 32 bit"
    If strRetVal="SK40728"
    Then
    If Not SetRegistryKeyValue(HKEY_LOCAL_MACHINE,"SOFTWARE\MapInfo\MapInfo\Professional\1150","LicenseServerName",REG_SZ,strData,iRetVal,strError) Then
    Note strError
    Else
    Print "Licensserver er opdateret til "+strData
    End If
    End If
    End If
    Exit Sub

    ReplyDelete
    Replies
    1. My pleasure Soren. I am glad that you found this post helpful :)

      Delete