IStream interface

G

Guest

Hi all,

I am developing website application in asp.net , visual C# and atl com. I
am using atl com component in visual C# application. One of the function of
com component interface returns IStream interface.

I want to read data from that IStream interface. I am new to visual c#. I
have written some code. But, it is returning whole data. It is returning some
null characters.

I have checked same com dll with vc++ client. It is returning whole data
properly.

VC++ code snippet (client):
//pI is com interface in vc++ client
IStream* pStream = NULL;
pI->TestStream(&pStream);
char* str1 = new char[30720];
ULONG rval = 0;
LARGE_INTEGER liBeggining = { 0 };
pStream->Seek(liBeggining,STREAM_SEEK_SET, NULL);
STATSTG statstg;
memset (&statstg, 0, sizeof(statstg));
hr = pStream -> Stat(&statstg, STATFLAG_NONAME);
pStream->Read(str1,30720,&rval);
pStream->Release();
int nFileHandle =
_open((LPCSTR)"c:\\atlcomdll\\chk1.txt",_O_CREAT|_O_BINARY|_O_RDWR|_O_TRUNC,_S_IREAD | _S_IWRITE);
_write(nFileHandle,str1,30720);
_close(nFileHandle);

delete[] str1;


Visual c# code snippet:

IStream pStream = null;
obj.TestStream(ref pStream);
tagSTATSTG statstg = new tagSTATSTG();
_LARGE_INTEGER liBeggining ;
liBeggining.QuadPart = 0;
_ULARGE_INTEGER newpos ;
newpos.QuadPart = 0;
pStream.RemoteSeek(liBeggining, 0, out newpos);
uint x = 0;
pStream.Stat(out statstg,x);
byte[] mybytearray = new byte[30720];
uint xy;
for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}

System.IO.FileStream objGetInfo =
System.IO.File.Create("C:\\ATLCOMDLL\\hi.txt");
objGetInfo.Write(mybytearray, 0, 30720);
objGetInfo.Close();


Now, I am comparing with chk1.txt and hi.txt. But, both are not same.

How to get the whole data in byte[] in visual c# using IStream interface?

suggest me.
 
W

Willy Denoyette [MVP]

John said:
Hi all,

I am developing website application in asp.net , visual C# and atl com.
I
am using atl com component in visual C# application. One of the function
of
com component interface returns IStream interface.

I want to read data from that IStream interface. I am new to visual c#. I
have written some code. But, it is returning whole data. It is returning
some
null characters.

I have checked same com dll with vc++ client. It is returning whole data
properly.

VC++ code snippet (client):
//pI is com interface in vc++ client
IStream* pStream = NULL;
pI->TestStream(&pStream);
char* str1 = new char[30720];
ULONG rval = 0;
LARGE_INTEGER liBeggining = { 0 };
pStream->Seek(liBeggining,STREAM_SEEK_SET, NULL);
STATSTG statstg;
memset (&statstg, 0, sizeof(statstg));
hr = pStream -> Stat(&statstg, STATFLAG_NONAME);
pStream->Read(str1,30720,&rval);
pStream->Release();
int nFileHandle =
_open((LPCSTR)"c:\\atlcomdll\\chk1.txt",_O_CREAT|_O_BINARY|_O_RDWR|_O_TRUNC,_S_IREAD
| _S_IWRITE);
_write(nFileHandle,str1,30720);
_close(nFileHandle);

delete[] str1;


Visual c# code snippet:

IStream pStream = null;
obj.TestStream(ref pStream);
tagSTATSTG statstg = new tagSTATSTG();
_LARGE_INTEGER liBeggining ;
liBeggining.QuadPart = 0;
_ULARGE_INTEGER newpos ;
newpos.QuadPart = 0;
pStream.RemoteSeek(liBeggining, 0, out newpos);
uint x = 0;
pStream.Stat(out statstg,x);
byte[] mybytearray = new byte[30720];
uint xy;
for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}

System.IO.FileStream objGetInfo =
System.IO.File.Create("C:\\ATLCOMDLL\\hi.txt");
objGetInfo.Write(mybytearray, 0, 30720);
objGetInfo.Close();


Now, I am comparing with chk1.txt and hi.txt. But, both are not same.

How to get the whole data in byte[] in visual c# using IStream interface?

suggest me.



Do you actually understand what you are doing here?

for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}
you are overwriting the buffer for each iteration, with a decreasing number
of bytes from the stream ...


You should read the whole stream at once just like you did in C++:

uint yz = 0;
pStream.RemoteRead(out mybytearray, sizeof(mybytearray), out
yz);

and check the number of bytes returned (yz) in the buffer against the size
of the buffer, if yz is smaller, then you are done reading, if equal you may
have more data to read...

Note that I can't comment on the remaining part of the code, as it's
incomplete, but there may be other issues.


Willy.
 
G

Guest

Hi Willy,

Thank you for your prompt response.

Is there any article to read the data from stream in visual c sharp
application for website.

I have replace with this below code according to your suggestion.
- uint xy;
pStream.RemoteRead(out mybytearray, 30720, out xy);

the output is :
----- Build started: Project: http://localhost/server1_test/, Configuration:
Debug .NET ------
Validating Web Site
Building directory '/server1_test/'.

f:\inetpub\wwwroot\server1_test\Default.aspx.cs(67,9): error CS1502: The
best overloaded method match for 'SERVER1Lib.IStream.RemoteRead(out byte,
uint, out uint)' has some invalid arguments
f:\inetpub\wwwroot\server1_test\Default.aspx.cs(67,32): error CS1503:
Argument '1': cannot convert from 'out byte[]' to 'out byte'
Validation Complete
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========


It is not allowing to take byte[]. It accepts only byte.

How do I resolve this?
-
Thanks & Regards,
John.


Willy Denoyette said:
John said:
Hi all,

I am developing website application in asp.net , visual C# and atl com.
I
am using atl com component in visual C# application. One of the function
of
com component interface returns IStream interface.

I want to read data from that IStream interface. I am new to visual c#. I
have written some code. But, it is returning whole data. It is returning
some
null characters.

I have checked same com dll with vc++ client. It is returning whole data
properly.

VC++ code snippet (client):
//pI is com interface in vc++ client
IStream* pStream = NULL;
pI->TestStream(&pStream);
char* str1 = new char[30720];
ULONG rval = 0;
LARGE_INTEGER liBeggining = { 0 };
pStream->Seek(liBeggining,STREAM_SEEK_SET, NULL);
STATSTG statstg;
memset (&statstg, 0, sizeof(statstg));
hr = pStream -> Stat(&statstg, STATFLAG_NONAME);
pStream->Read(str1,30720,&rval);
pStream->Release();
int nFileHandle =
_open((LPCSTR)"c:\\atlcomdll\\chk1.txt",_O_CREAT|_O_BINARY|_O_RDWR|_O_TRUNC,_S_IREAD
| _S_IWRITE);
_write(nFileHandle,str1,30720);
_close(nFileHandle);

delete[] str1;


Visual c# code snippet:

IStream pStream = null;
obj.TestStream(ref pStream);
tagSTATSTG statstg = new tagSTATSTG();
_LARGE_INTEGER liBeggining ;
liBeggining.QuadPart = 0;
_ULARGE_INTEGER newpos ;
newpos.QuadPart = 0;
pStream.RemoteSeek(liBeggining, 0, out newpos);
uint x = 0;
pStream.Stat(out statstg,x);
byte[] mybytearray = new byte[30720];
uint xy;
for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}

System.IO.FileStream objGetInfo =
System.IO.File.Create("C:\\ATLCOMDLL\\hi.txt");
objGetInfo.Write(mybytearray, 0, 30720);
objGetInfo.Close();


Now, I am comparing with chk1.txt and hi.txt. But, both are not same.

How to get the whole data in byte[] in visual c# using IStream interface?

suggest me.



Do you actually understand what you are doing here?

for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}
you are overwriting the buffer for each iteration, with a decreasing number
of bytes from the stream ...


You should read the whole stream at once just like you did in C++:

uint yz = 0;
pStream.RemoteRead(out mybytearray, sizeof(mybytearray), out
yz);

and check the number of bytes returned (yz) in the buffer against the size
of the buffer, if yz is smaller, then you are done reading, if equal you may
have more data to read...

Note that I can't comment on the remaining part of the code, as it's
incomplete, but there may be other issues.


Willy.
 
W

Willy Denoyette [MVP]

John said:
Hi Willy,

Thank you for your prompt response.

Is there any article to read the data from stream in visual c sharp
application for website.

I have replace with this below code according to your suggestion.
- uint xy;
pStream.RemoteRead(out mybytearray, 30720, out xy);

the output is :
----- Build started: Project: http://localhost/server1_test/,
Configuration:
Debug .NET ------
Validating Web Site
Building directory '/server1_test/'.

f:\inetpub\wwwroot\server1_test\Default.aspx.cs(67,9): error CS1502: The
best overloaded method match for 'SERVER1Lib.IStream.RemoteRead(out byte,
uint, out uint)' has some invalid arguments
f:\inetpub\wwwroot\server1_test\Default.aspx.cs(67,32): error CS1503:
Argument '1': cannot convert from 'out byte[]' to 'out byte'
Validation Complete
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped
==========


It is not allowing to take byte[]. It accepts only byte.

How do I resolve this?
-
Thanks & Regards,
John.


Willy Denoyette said:
John said:
Hi all,

I am developing website application in asp.net , visual C# and atl
com.
I
am using atl com component in visual C# application. One of the
function
of
com component interface returns IStream interface.

I want to read data from that IStream interface. I am new to visual
c#. I
have written some code. But, it is returning whole data. It is
returning
some
null characters.

I have checked same com dll with vc++ client. It is returning whole
data
properly.

VC++ code snippet (client):
//pI is com interface in vc++ client
IStream* pStream = NULL;
pI->TestStream(&pStream);
char* str1 = new char[30720];
ULONG rval = 0;
LARGE_INTEGER liBeggining = { 0 };
pStream->Seek(liBeggining,STREAM_SEEK_SET, NULL);
STATSTG statstg;
memset (&statstg, 0, sizeof(statstg));
hr = pStream -> Stat(&statstg, STATFLAG_NONAME);
pStream->Read(str1,30720,&rval);
pStream->Release();
int nFileHandle =
_open((LPCSTR)"c:\\atlcomdll\\chk1.txt",_O_CREAT|_O_BINARY|_O_RDWR|_O_TRUNC,_S_IREAD
| _S_IWRITE);
_write(nFileHandle,str1,30720);
_close(nFileHandle);

delete[] str1;


Visual c# code snippet:

IStream pStream = null;
obj.TestStream(ref pStream);
tagSTATSTG statstg = new tagSTATSTG();
_LARGE_INTEGER liBeggining ;
liBeggining.QuadPart = 0;
_ULARGE_INTEGER newpos ;
newpos.QuadPart = 0;
pStream.RemoteSeek(liBeggining, 0, out newpos);
uint x = 0;
pStream.Stat(out statstg,x);
byte[] mybytearray = new byte[30720];
uint xy;
for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}

System.IO.FileStream objGetInfo =
System.IO.File.Create("C:\\ATLCOMDLL\\hi.txt");
objGetInfo.Write(mybytearray, 0, 30720);
objGetInfo.Close();


Now, I am comparing with chk1.txt and hi.txt. But, both are not same.

How to get the whole data in byte[] in visual c# using IStream
interface?

suggest me.



Do you actually understand what you are doing here?

for (uint i = 0; i < 30720; i ++)
{
uint yz = 0;
pStream.RemoteRead(out mybytearray, i, out yz);
}
you are overwriting the buffer for each iteration, with a decreasing
number
of bytes from the stream ...


You should read the whole stream at once just like you did in C++:

uint yz = 0;
pStream.RemoteRead(out mybytearray, sizeof(mybytearray), out
yz);

and check the number of bytes returned (yz) in the buffer against the
size
of the buffer, if yz is smaller, then you are done reading, if equal you
may
have more data to read...

Note that I can't comment on the remaining part of the code, as it's
incomplete, but there may be other issues.


Willy.
 
W

Willy Denoyette [MVP]

John said:
Hi Willy,

Thank you for your prompt response.

Is there any article to read the data from stream in visual c sharp
application for website.

I have replace with this below code according to your suggestion.
- uint xy;
pStream.RemoteRead(out mybytearray, 30720, out xy);

the output is :
----- Build started: Project: http://localhost/server1_test/,
Configuration:
Debug .NET ------
Validating Web Site
Building directory '/server1_test/'.

f:\inetpub\wwwroot\server1_test\Default.aspx.cs(67,9): error CS1502: The
best overloaded method match for 'SERVER1Lib.IStream.RemoteRead(out byte,
uint, out uint)' has some invalid arguments
f:\inetpub\wwwroot\server1_test\Default.aspx.cs(67,32): error CS1503:
Argument '1': cannot convert from 'out byte[]' to 'out byte'
Validation Complete
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped
==========


It is not allowing to take byte[]. It accepts only byte.

How do I resolve this?
-
Thanks & Regards,
John.


As I told you it's hard to answer such questions if you only post a part of
the code, what I'm missing here is your RemoteRead declaration.
Anyway, try this:

pStream.RemoteRead(mybytearray, 30720, out xy);

Also, don't use hard coded values, pass the size of the byte[] like this:

pStream.RemoteRead(mybytearray, mybytearray.Length, out xy);

and use meaningfull and "correctly" cased names in your code....

pStream.RemoteRead(myByteArray, myByteArray.Length, out bytesReturned);

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top