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