tags:

views:

381

answers:

2

Hi,

I am exporting a function from vc++ DLL to write to a binary file. In the C++ code the file is opened using

FILE* fp = ::_tfopen (FilePath, _T("a+b"));

I a using the "a+b" mode to append the file later on and b is for the binary mode.

Now I am importing this function in my C# application. When I make a call to this function from C# with the right arguments, the file gets written but not in append mode. What I mean is that the function opens the file but doesnt seem to append to its contents , instead it destroys its contents and then writes whatever my C# arguments are.

Platform is VS2005. Any help please?

- Thanks Viren

Imported from comments

[DllImport("NameOfTheDLL.dll", CharSet = CharSet.Ansi)] 
public static extern int function_name(IntPtr ptr, 
   [MarshalAs(UnmanagedType.LPWStr)] string FilePath);

Following is the call to the function:

IntPtr ptr = some_Init_function(); 
function_name(handle, "C:\\FileName");
+1  A: 

how your C# function looks like?

please attach the enum

FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
Angel Escobedo
[DllImport("NameOfTheDLL.dll", CharSet = CharSet.Ansi)]public static extern int function_name(IntPtr ptr, [MarshalAs(UnmanagedType.LPWStr)] string FilePath);Following is the call to the function:IntPtr ptr = some_Init_function();function_name(handle, C:\\FileName);
VP
The file is not opened by the C# application. So I dont get whwy the FileMode.oPEN IS REQUIRED. tHE FILE IS OPENED IN THE c++ dll.Please guide me if I am wrong. I am new to .NET programming
VP
By the way:StreamReader sr = new StreamReader(FilePath)is what I am using to read from the binary file
VP
A: 

Does the C++ function work as intended when NOT being imported into C#? The problem might be with the code in the DLL itself.

Charlie Salts
No I just checked the same. It works fine when called from within the C++ dll. The problem is when called from C#.
VP
maybe the typed IntPtr on C# is sending Null
Angel Escobedo