Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2194
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T17:52:09+00:00 2022-05-31T17:52:09+00:00

winapi – Access is denied error while moving file using MoveFileEx on windows

  • 0

[ad_1]

I am using MoveFileEx windows api to move existing file to new file.
Also executing below program multiple times via script on windows.

Below program creates unique file on every execution and moves it to some file say FinalCAFile.txt.
But for some process execution getting error as “Access is denied“.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <process.h>

using namespace std;

#define BUFSIZE 1024

void PrintError(LPCTSTR errDesc,string process_id);


int main(int argc, TCHAR* argv[])
{
    BOOL fSuccess = FALSE;

    //Get process id to create unique file
    std::string process_id = std::to_string(_getpid());

    std::string tempCaFile = "C://Users//Administrator//source//repos//MyProgram//Debug//tempCAFile" + process_id + ".txt";;
    std::string finalCaFile = "C://Users//Administrator//source//repos//MyProgram//Debug//FinalCAFile.txt";

    //Create unique temp CA file 
    std::ofstream file(tempCaFile);
    std::string my_string = "Hello from process " + process_id;
    file << my_string;

    std::wstring sourceTempCAFile = std::wstring(tempCaFile.begin(), tempCaFile.end());
    std::wstring finalCAFile = std::wstring(finalCaFile.begin(), finalCaFile.end());

    file.close();

    //move temporary created file to FinalCAFile.txt
    fSuccess = MoveFileEx(sourceTempCAFile.c_str(),
                finalCAFile.c_str(),
                MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
    if (!fSuccess)
    {
        cout<<endl<<"\nMoveFileEx failed for"<<process_id;
        file.close();
        PrintError(TEXT("MoveFileEx failed"),process_id);
        return (9);
    }
    else
    {
        cout << endl<<"\nMoveFileEx Success for " << process_id;
        std::string passedFileName = "C://Users//Administrator//source//repos//MyProgram//Debug//PassedFile" + process_id + ".txt";
        std::ofstream passedFile(passedFileName);
        std::string my_string = "Passed for process id: " + process_id;
        passedFile << my_string;
        passedFile.close();
    }

    file.close();
    return (0);
}

//  ErrorMessage support function.
//  Retrieves the system error message for the GetLastError() code.
//  Note: caller must use LocalFree() on the returned LPCTSTR buffer.
LPCTSTR ErrorMessage(DWORD error, string process_id)
{
    LPVOID lpMsgBuf;
    //error = 5;
    printf("\nDWORD=%d", (unsigned int)error);
    std::string failedFileName = "C://Users//Administrator//source//repos//MyProgram//Debug//FailedFile" + process_id + ".txt";
    std::ofstream failedFile(failedFileName);
    
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
        | FORMAT_MESSAGE_FROM_SYSTEM
        | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        error,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&lpMsgBuf,
        0,
        NULL);

    std::string my_string = "Failed for process id: " + process_id + " due to " + std::to_string((unsigned int)error) ;
    failedFile << my_string;


    return((LPCTSTR)lpMsgBuf);
}

//  PrintError support function.
//  Simple wrapper function for error output.
void PrintError(LPCTSTR errDesc, string process_id)
{
    LPCTSTR errMsg = ErrorMessage(GetLastError(), process_id);
    _tprintf(TEXT("\n** ERROR ** %s: %s\n"), errDesc, errMsg);
    Sleep(10000);
    LocalFree((LPVOID)errMsg);
}


Script- MyScript.bat
FOR /L %%i IN (1,1,2) DO (
   start  C:\Users\Administrator\source\repos\ConsoleApplication4\Debug\MyProgram.exe
)

As per my understanding we get “Access is denied” error if there is write permission issue.

But here in my case executing same program multiple times, so dont know how permission issue coming into picture .
So there seems to be some synchronization issue.
Also there may be possibility that this MoveFileEx dont support proper locking mechanism internally.
I doubt if there is synchronization issue with MoveFileEx, then others might also get this issue .

Any suggestion to avoid this issue ?

Below is the script file –
FOR /L %%i IN (1,1,50) DO (
start C:\Users\Administrator\source\repos\ConsoleApplication4\Debug\ConsoleApplication4.exe
)

This above script starts executing MyProgram 50 times, and on some execution few processes giving issue as “Access is denied”.

Result is inconsistent, not for every script run i get mentioned error, but after 2-3 run able to get the issue.

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4998 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1084 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1028 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.