matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: unicode problem
code: /*
_readFile coded by Matty
This function will read the contents of a file and print out the contents regardless of being unicode or ansi.
If you use this function please make sure to give credit where credit is due.
*/
function OnEvent_Initialize(MessengerStart) {
_readFile('C:\\WndSelect.handler.js', true);
}
function _readFile(_s_file, _is_debugging){
/* Use the windows api function CreateFileW to open the file ONLY if it already exists */
var _h_file = Interop.Call('kernel32', 'CreateFileW', '\\\\?\\'+_s_file,
0x80000000 /* GENERIC_READ */,
0x01 /* FILE_SHARE_READ */,
0,
3 /* OPEN_EXISTING */,
0,
0);
/* Check to make sure the pointer is actually to a file instead of an invalid handle */
if (_h_file > -1 /* INVALID_HANDLE_VALUE */) {
if (_is_debugging === true) Debug.Trace('_h_file : '+_h_file);
/* Now that we have a pointer to the file its time to read the size of the file so we know how much to read */
var _n_size = Interop.Call('kernel32', 'GetFileSize', _h_file, 0);
if (_is_debugging === true) Debug.Trace('_n_size : '+_n_size);
/* Create a buffer to read in the data and another buffer to store the number of bytes that have been read */
var _n_buffer = Interop.Allocate((2*_n_size)+2);
var _n_bytes_read = Interop.Allocate(4);
/* Read the file */
Interop.Call('kernel32', 'ReadFile', _h_file, _n_buffer, _n_size, _n_bytes_read, 0);
/* Close the file */
Interop.Call('kernel32', 'CloseHandle', _h_file);
/* Check if the file is unicode, if it is read the contents of _n_buffer as unicode, otherwise read it as ansi */
if (_n_buffer.ReadString(0, false) === '˙ūv'){
if (_is_debugging === true) Debug.Trace('_n_buffer : '+_n_buffer.ReadString(0, true));
return _n_buffer.ReadString(0, true);
} else {
if (_is_debugging === true) Debug.Trace('_n_buffer : '+_n_buffer.ReadString(0, false));
return _n_buffer.ReadString(0, false);
}
}
}
This post was edited on 02-03-2007 at 05:50 PM by matty.
|
|