How to do this in VB .Net. Creating a file handle?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm wondering if I can do this with a streamWriter? But I'm not sure what
this converts too.
I'm trying to recreat this code in Visual Basic .Net. I have C++ code that
looks like this:
{...
theStatus = CreateDestinationFile(theFileName, &theFileRef);
....}
....
KPDCStatus CreateDestinationFile(char* fileName, KPDCFileRefHandle* fileRef)
{
KPDCStatus theStatus = KPDC_OK;
char theFilePath[MAX_STRING_LENGTH];
KPDCUInt32 theAttributeSize = 0;
HANDLE theFileHandle = INVALID_HANDLE_VALUE;

//
// Set up the file path
//
strcpy(theFilePath, "C:\\");
strcat(theFilePath, fileName);

//
// Create the file
//
theFileHandle = CreateFile(theFilePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(theFileHandle == INVALID_HANDLE_VALUE)
{
theStatus = (KPDCStatus) GetLastError();
}
else
{
*fileRef = (KPDCFileRefHandle) theFileHandle;
}

return(theStatus);
}
....
#define EXPAND_LOCAL_DRIVES

WINBASEAPI
HANDLE
WINAPI
CreateFileA(
IN LPCSTR lpFileName,
IN DWORD dwDesiredAccess,
IN DWORD dwShareMode,
IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
IN DWORD dwCreationDisposition,
IN DWORD dwFlagsAndAttributes,
IN HANDLE hTemplateFile
);
WINBASEAPI
HANDLE
WINAPI
CreateFileW(
IN LPCWSTR lpFileName,
IN DWORD dwDesiredAccess,
IN DWORD dwShareMode,
IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
IN DWORD dwCreationDisposition,
IN DWORD dwFlagsAndAttributes,
IN HANDLE hTemplateFile
);
 
MK,
Looking at that code certainly gives an appreciation for C# and
the .Net framework! Looks like you are creating a file handle that is
then to be used by calling code to write to? In .Net we would not deal
with a file handle directly unless we had to use win32 API in .Net a
file handle is wrapped in an instance of the FileStream class. If you
instaniate a StreamWriter with a path in it's constructor it will
create a FileStream internally for you and allow you to write to it.
You can also control how the FileStream is created by creating one
yourself explicitly and then pass it to the constructor of the
StreamWriter class. Here is a very simple example of the former:
Dim sw As New System.IO.StreamWriter("C:\Test.txt")
sw.WriteLine("This is a line")
sw.Close()

Does this help or did I miss the point of your question?
Cecil Howell MCSD, MCAD.Net, MCT
 
Yea, I kinda knew that. But this code is only part of what I need to do. I
need to pass the file handle to unmanaged code. So, I guess I am wondering,
if I create a FileStream object, and pass it to the unmanaged code, will
P/Invoke marshal it correctly?
 
Almost any .Net class that wraps a windows handle will expose it with
a handle property of type IntPtr which you can pass to unmanaged code.
Use the Handle property og the FileStream.
Cecil Howell MCSD, MCAD.Net, MCT
 
thank you thank you thank you... This is helping... But still some more
questions.
Okay, so theFileRef = fileStream1.Handle. The demo code I'm following does
this:

//
// Allocate SDK IO reference for the open destination file
//
theStatus = theProcs.AllocIORefFromFileHandleRef(theLibMgr, theFileRef,
&theDstIO);

//
// Allocate the tranfser buffer
//
theBuffer = malloc(TRANSFER_BLOCK_SIZE); //'<---Can I do this?

for(theBlockIndex = 0; theBlockIndex < theFullBlockCount; theBlockIndex++)
{
//
// Read from the camera's memory
//
theStatus = theProcs.Read(theSrcIO, TRANSFER_BLOCK_SIZE, theBuffer);

//
// Write to the host file
//
theStatus = theProcs.Write(theDstIO, TRANSFER_BLOCK_SIZE, theBuffer);
}

So, the SDK (umamaged code) creates an IOReference for the data on the
camera, and one for the new file on the hard drive, then writes a block at a
time.

It uses theBuffer, which is a BlockSize of 0x80000 (524288bytes?). Can I,
how do I, do that? I wish they just let you access the memory card like
everybody else does!!!
 
// Pointer to tranfser buffer
unsigned char* theBuffer = NULL;

In .Net (Visual Basic) how do I do that?
 
Thanks again for the response. So, here's my code:
Dim theBuffer(524288) As Byte
'//
'// Read from the camera's memory
'//
myStatus = Me.KRead(mySrcIO, theBuffer.Length, theBuffer)

'//
'// Write to the host file
'//
myStatus = Me.KWrite(myTrgIO, theBuffer.Length, theBuffer)

KRead is unmanaged function that reads from mySrcIO to theBuffer. KWrite
writes from the buffer to myTrgIO.

The signature for KRead and KWrite is as follows:
KPDCStatus KPDCRead( KPDCIORef inIORef, KPDCUInt32 inCount, KPDCUInt8
*outBuffer);
KPDCStatus KPDCWrite( KPDCIORef inIORef, KPDCUInt32 inCount, KPDCUInt8
*inBuffer);

So the def asks for an Unsigned Int. But the demo uses an unsigned char. But
the def also looks like it's looking for a handle, so I'm not sure how to do
that.
 
If the first arg of both functions should be a handle I assume you
meant inIORef.Handle. So does this work? If not what is the error
message?

Cecil Howell MCSD, MCAD.Net, MCT
 
Back
Top