This website uses cookies. By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies.
Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
- Revenera Community
- :
- InstallShield
- :
- InstallShield Forum
- :
- Re: FileOpen failure
Subscribe
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
‎May 26, 2010
10:23 AM
FileOpen failure
Basic MSI project here, and I'm trying to open a text file to edit, but for some reason the OpenFile function always fails when trying to open the file. I don't know why it's failing. My function is being called by another function in the InstallScript file, but the failure occurs here. Here is the code.
The message pops up about failing to open the file, but I know it exists and isn't locked. OpenFile fails both when C:\logs\test.txt is already created and when it isn't. I tried changing the OpenFileMode to FILE_MODE_NORMAL and it still fails to open.
I'm not sure what's going wrong here, this seems like a fairly simple thing to do, yet it is strangely not working. Any help please?
prototype openLogFile();
function openLogFile()
NUMBER nvFileHandle;
STRING dir, file;
begin
dir = "C:\\logs";
file = "test.txt";
OpenFileMode(FILE_MODE_APPEND);
if(ExistsDir(dir) < 0) then
CreateDir(dir);
endif;
if(Is(FILE_EXISTS, dir^file) < 0) then
MessageBox("File doesn't exist" ,0);
endif;
if(Is(FILE_LOCKED, dir^file) < 0) then
MessageBox("File is locked", 0);
endif;
if(OpenFile(nvFileHandle, dir, file) < 0) then
MessageBox("Failed to open " + dir^file, 0);
endif;
return nvFileHandle;
end;
The message pops up about failing to open the file, but I know it exists and isn't locked. OpenFile fails both when C:\logs\test.txt is already created and when it isn't. I tried changing the OpenFileMode to FILE_MODE_NORMAL and it still fails to open.
I'm not sure what's going wrong here, this seems like a fairly simple thing to do, yet it is strangely not working. Any help please?
(5) Replies
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
‎May 26, 2010
03:17 PM
Are you running this code from an immediate custom action? If so, you may not have permissions to create or open files in this location (C:\ tends to require admin privileges, so do most direct subfolders). In this case, the file either needs to be created in a location you have permissions to, the permissions on this folder need to be changed, or the action needs to run deferred in system context.
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
‎May 26, 2010
04:55 PM
Thanks for the response. I am running it as a Deferred Custom Action. I have permission to write to files in this location. I've been able to get ConfigFileLoad and the associated configuration file functions to write to a file and work correctly. I'm not sure why OpenFile would fail? I feel like I'm at a brick wall here.
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
‎May 27, 2010
11:11 AM
Since the OpenFile function does not return any system error codes on failure to open a file, there are two ways you can try to troubleshoot this behavior:
- Use the Process Monitor utility (from www.sysinternals.com) to monitor msiexec.exe and watch for access to the file being passed to OpenFile. Process Monitor will indicate the Win32 API status returned when attempting to open the file.
- Use the following code in your script at the point you normally call OpenFile:
The above CreateFile call is effectively the same call generated when calling the OpenFile function with the file mode set to FILE_MODE_APPEND. If the CreateFile call fails, the SprintfBox in the following if will display the Win32 error status set by CreateFile. This should provide some indication of why OpenFile is failing.
- Use the Process Monitor utility (from www.sysinternals.com) to monitor msiexec.exe and watch for access to the file being passed to OpenFile. Process Monitor will indicate the Win32 API status returned when attempting to open the file.
- Use the following code in your script at the point you normally call OpenFile:
nvResult = CreateFileA("C:\\logs\\test.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | 0x2, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(nvResult == INVALID_HANDLE_VALUE) then
SprintfBox(0, "CreateFile error", "Win32 CreateFile last error: %d", Err.LastDllError);
else
CloseHandle(nvResult);
endif;
The above CreateFile call is effectively the same call generated when calling the OpenFile function with the file mode set to FILE_MODE_APPEND. If the CreateFile call fails, the SprintfBox in the following if will display the Win32 error status set by CreateFile. This should provide some indication of why OpenFile is failing.
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
‎May 27, 2010
12:35 PM
I used the CreateFile call, and it worked! At least, I didn't get any errors. So is nvResult the file handler? I tried passing it into WriteLine, and I didn't get any errors, but it's not writing to the file. The CreateFile call you gave me looks like it allows the file to be modified correct?
But this seems to be opening the file correctly. Thanks for your help!
But this seems to be opening the file correctly. Thanks for your help!
- Mark as New
- Subscribe
- Mute
- Permalink
- Report Inappropriate Content
‎Jun 01, 2010
01:52 PM
The CreateFile call is calling the Win32 API directly instead of going through the InstallScript file functionality. The handle returned isn't interchangeable with other InstallScript file functions. The code I posted essentially is what the InstallScript OpenFile function will call internally.
At any other point in your script, are you opening or creating this test.txt file? Since the file could be opened directly by Windows, it is possible that some other checks done by InstallScript's OpenFile could be the cause of the failure.
At any other point in your script, are you opening or creating this test.txt file? Since the file could be opened directly by Windows, it is possible that some other checks done by InstallScript's OpenFile could be the cause of the failure.