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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:49:03+00:00 2026-06-03T05:49:03+00:00

I have been trying to write and read files through P/Invoke via my ASP.NET

  • 0

I have been trying to write and read files through P/Invoke via my ASP.NET website. I am facing a problem as to where the files are written/read when doing this through dlls in a website. I have tried to explain the problem with the below example:

.cpp file (containing a read and write function)

extern "C" TEST_API int fnTest(char* fileDir)
{
ofstream myfile;
myfile.open (strcat(fileDir, "test.txt"));
myfile << "Writing this to a file.\n";
myfile.close();
}

extern "C" TEST_API char* fnTest1(char* fileDir)
{
ifstream myReadFile;
myReadFile.open(strcat(fileDir, "test1.txt"));
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
    myReadFile >> output;
return output;
}

Post build event of website to copy the dll’s from above C++ project to website’s bin folder

Default.aspx.cs – C#
Dll functions

public static class Functions(){
DllImport[("Test1.dll", EntryPoint="fnTest", CharSet=CharSet.Ansi]
public static extern int fnTest(string dir);

DllImport[("Test1.dll", EntryPoint="fnTest1", CharSet=CharSet.Ansi]
public static extern StringBuilder fnTest1(string dir);
}

Page_Load event

string direc = AppDomain.CurrentDomain.BaseDirectory + "bin\\";
string txt1 = Functions.fnTest(direc).ToString(); //failing here - keeps on loading the page forever
string txt2 = Functions.fnTest(direc).ToString(); //failing here - keeps on loading the page forever

If I try the same Page_Load code in a desktop application with direc being set as current directory of the project output, everything works fine. It’s only that the directories where the files are to be written or read are kind of messed in case of web site and I am not really able to figure out how to correct this and get it working. Suggestions would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:49:04+00:00Added an answer on June 3, 2026 at 5:49 am

    You still have a number of the same problems as you have in the last question.

    This time round your biggest problem is here:

    strcat(fileDir, "test.txt")
    

    You can’t modify fileDir since it is owned by the pinvoke marshaller. Instead of passing the directory to your native code, pass the full path to the file. Use Path.Combine in your managed code to create that, and pass it to the native code.

    extern "C" TEST_API int fnTest(char* filename)
    {
        ofstream myfile;
        myfile.open(filename);
        myfile << "Writing this to a file.\n";
        myfile.close();
    }
    

    and in managed code

    string filename = Path.Combine(
        AppDomain.CurrentDomain.BaseDirectory, "bin", "test.txt");
    string txt1 = Functions.fnTest(filename).ToString(); 
    

    In a comment you explain that you need to concatenate strings in the native code. You will need to create a native string to do that because you are not allowed to write to fileDir. Something like this:

    string fileName = string(fileDir) + "test.txt";
    myfile.open(fileName.c_str());
    

    But you still need to fix fnTest1 which reads the file. My answer at your other question tells you how to do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write and read files directly from the BlobStore, but
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write an image on a layer using Quartz but
I have been trying to write my own diff3 wrap script for SVN and
I have been trying to write a regex that will remove whitespace following a
I have been trying to write a custom .screenrc file TEST as follows startup_message
I have been trying to write a shortest path algorithm, dijkstras algorithm, finding the
I am new to programming. I have been trying to write a function in
Right now I have been trying to use Launchpad's API to write a small
Have have been trying to make a validator for my xml files. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.