Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the regional settings in the registry 3

Status
Not open for further replies.

Zeroanarchy

Technical User
Jun 11, 2001
630
AU
Has anyone tried to edit the short date in the regional settings via the registry? I am trying to automate a procedure that changes the regional settings for a computer to dd/m/yyyy or in XP to English(Australia).

Thanks Zeroanarchy
 

Public Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long

Public Const LOCALE_SSHORTDATE As Long = &H1F 'short date format string

Public Const DATE_SHORTDATE As Long = &H1

Sub ChangeLocalDate(newFormat As String
)

Dim sdate As Long

'set the new long date format
Call SetLocaleInfo(LCID, sdate, newFormat)

 
Hey, I wasn't finished!

Public Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long

Public Const LOCALE_SSHORTDATE As Long = &H1F 'short date format string
Public Const DATE_SHORTDATE As Long = &H1

Public Declare Function GetUserDefaultLCID Lib "kernel32" () As Long

Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_SETTINGCHANGE As Long = &H1A

Sub ChangeLocalDate(newFormat As String)

Dim LCID As Long

LCID = GetUserDefaultLCID()

'set the new long date format
Call SetLocaleInfo(LCID, LOCALE_SSHORTDATE, newFormat)
'send a system notification message that a change was made
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)

End Sub

Please note: I was only copying and pasting lines of code from my lib, so I haven't tested this. But you should get the idea...
Of course, you may want to add some error handling here
 
CCLINT, is it possible to get the current format i.e "dd/mm/yyyy"

what i want to know is what format the user machine has, so the function has to return a string like "dd/mm/yyyy" or whatever.

Is this possible, may be using "GetLocaleInfo"
i tired to search but could not get any info
any ideas on this

[cheers]
Niraj [noevil]
 
ufobaby: GetLocaleInfo is correct:


Public Declare Function GetLocaleInfo Lib "kernel32" _
Alias "GetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As Long

Function GetUserLocaleInfo() As String
Dim LCID As Long 'Local ID
Dim sReturnBuffer As String 'string buffer
Dim lReturn As Long 'buffer size
Dim lKey As Long 'Get What?

LCID = GetUserDefaultLCID()
lKey = LOCALE_SSHORTDATE

'get the required size of the string buffer
lReturn = GetLocaleInfo(LCID, lKey, sReturnBuffer, Len(sReturnBuffer))

If lReturn Then
'pad the buffer with the needed spaces
sReturnBuffer = Space$(lReturn)

'Call GetLocaleInfo again this time passing the buffer
lReturn = GetLocaleInfo(LCID, lKey, sReturnBuffer, Len(sReturnBuffer))
If lReturn Then

'lreturn: size of the string including the terminating null
GetUserLocaleInfo = Left$(sReturnBuffer, lReturn - 1)

End If
End If

End Function

If you need to get more info than this, the I would suggest to pass the lKey to this proceedure. You can get all the constants which can be assigned to the lKey from the API viewer (number format, date/decimal seperators, Long Date format, etc.). Just copy the constants and pass them one at a time to this function:

Function GetUserLocaleInfo(lKey As Long) As String

Like this:
Dim sLocalFormat As String
sLocalFormat = GetUserLocaleInfo(LOCALE_SSHORTDATE)
 
hi CCLINT,

GR8 .... works perfect, that was a great help....

will also try the other option of to get more info on the same

but tell me i tired on allpai.net and also microsoft but could not get a proper info... where did u get this , and more ????

Thanks a ton once again !!!

[cheers]
Niraj [noevil]
 
You're welcome.
You should find plenty of info by doing a search in the knowledge base using the name of the API function call....
 
CCLINT thanks for your quick reply,I do not have a high level of experties in relation to VB, so far I have created a module and placed the code into it.I then created a form

on load
call GetUserDefaultLCID Lib

and nothing happens. two questions is there a prob with the way I have designed it or could it be this method does not work in Windows XP.

Thanks for your help
ZeroAnarchy
 
GetUserDefaultLCID is the API function to get the country code, which is used here by the SetLocaleInfo API function. I think you have done something wrong. Make sure you use my second post, and not the first post which wasn't finished.

Please note that the call that you should be making is to the sub proceedure that I wrote above:
ChangeLocalDate strNewFormat
ín order to change the format.

For the third post that I made, which returns the value of the current format using GetUserLocaleInfo, you need to call it as follows:


strSysShortDateFormat = GetUserLocaleInfo()

where you declare the string variable strSysShortDateFormat with-in the desired scope (Form level, or sub proceedure level)

The second one was tested above by ufobaby. So try and see if the second one - which returns the format as opposed to setting it - works for you.

Also please note that if you put both code examples in the same module then it would be best to declare everything Private, except for the two proceedures:
GetUserLocaleInfo
ChangeLocalDate
Which should be declare as Public.

I just tested the GetUserLocaleInfo and ChangeLocalDate (which actually should be called something like "ChangeLocalShortDateFormat"), and it is working.
In the Debug window:
?GetUserLocaleInfo
(placing the cursor at the end of the line: ?GetUserLocaleInfo
and press Enter)

returns MM/dd/yy

ChangeLocalDate "MM/dd/yyyy"

?GetUserLocaleInfo
returns MM/dd/yyyy

And:
?GetUserDefaultLCID
returns the country code.

Try these in the debug window first to see it eveything is working.
 
sorry to pain I am getting the following error:
Compile error:
ByRef argument type mismatch

my Form has the following code:

Private Sub Form_Load()
Call ChangeLocalDate(strNewFormat)
End Sub

Am I missing a reference? or have I made a silly mistake somewhere.

In the module I have the second lot of code you posted.


Public Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long

Public Const LOCALE_SSHORTDATE As Long = &H1F 'short date format string
Public Const DATE_SHORTDATE As Long = &H1

Public Declare Function GetUserDefaultLCID Lib "kernel32" () As Long

Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_SETTINGCHANGE As Long = &H1A

Sub ChangeLocalDate(newFormat As String)

Dim LCID As Long

LCID = GetUserDefaultLCID()

'set the new long date format
Call SetLocaleInfo(LCID, LOCALE_SSHORTDATE, newFormat)
'send a system notification message that a change was made
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)

End Sub


Thanks Zero
 
You didn't declare strNewFormat, or rather, you didn't assign a value to it(I thought this would be visible enough that it represents a string that you need to assign a value to):

Private Sub Form_Load()
Dim strNewFormat As String
strNewFormat = "MM/dd/yyyy"
Call ChangeLocalDate(strNewFormat)
End Sub

you could just use:
ChangeLocalDate "MM/dd/yyyy"
as in my example to test above.

And, it would be a bad idea to change the line:
Sub ChangeLocalDate(newFormat As String)
to:
Sub ChangeLocalDate(Byval newFormat As String)

If all of this is done and you forget to actually pass something, instead of getting an error, nothing should happen - I mean the format will remain the same and the program will continue.

Also, you need to use Option Explicit at the top of each form, module and class. Then you would have first found out that strNewFormat needed to be declared.
 
lol, thank you very much for your help CCLINT greatly appreatiated. Works great, are you aware of any probs with this method in diffrent Win versions or should it work on all of them?
 
You're using XP? Not to my knowledge.
Maybe strongm can answer that better than I...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top