TROUBLE Passing BYTE Array from Client to COM OUTPROC Server [modified]
-
Hi Stephen, What do you mean by Code highlights?
-
Ya i did the same way by copying you code.Still not able to access the entire array. Can you send me a working client server codes developed in VC98 or VS2003? My Id: k_arunkumar_1217@yahoo.com
-
Ya i did the same way by copying you code.Still not able to access the entire array. Can you send me a working client server codes developed in VC98 or VS2003? My Id: k_arunkumar_1217@yahoo.com
No, I mean paste in the important sections of your code here (like I did on the post I linked to).
Steve
-
No, I mean paste in the important sections of your code here (like I did on the post I linked to).
Steve
IDL File Entry: [id(5),helpstring("method SetData")] HRESULT SetData([in] ULONG size, [in,size_is(size)] BYTE *pData); Method Override: STDMETHODIMP CDataProvider::SetData(ULONG size, BYTE* bytArray) { // TODO: Add your implementation code here BYTE byt[6]; int i=0; for(;*bytArray!=0;i++) { byt[i]=*bytArray; ::MessageBox(NULL,(LPCTSTR)"he","Msg",MB_OK); bytArray++; } byt[i]=0; return S_OK; } Client: IDataProvider* pServer=NULL; HRESULT hr=::CoCreateInstance(CLSID_CDataProvider,NULL,CLSCTX_LOCAL_SERVER,IID_IDataProvider,(LPVOID*)&pServer); short sSize = 5; BYTE* pBytes; //Allocate the shared Memory pBytes = reinterpret_cast(CoTaskMemAlloc(sSize * sizeof(BYTE))); if(pBytes == NULL) return; ::memset(pBytes, 0, sSize); pBytes[0]='K'; pBytes[1]='K'; pBytes[2]='R'; pBytes[3]='D'; pBytes[4]=NULL; pServer->SetData(5,pBytes); CoTaskMemFree(pBytes); //Free the shared Memory
-
IDL File Entry: [id(5),helpstring("method SetData")] HRESULT SetData([in] ULONG size, [in,size_is(size)] BYTE *pData); Method Override: STDMETHODIMP CDataProvider::SetData(ULONG size, BYTE* bytArray) { // TODO: Add your implementation code here BYTE byt[6]; int i=0; for(;*bytArray!=0;i++) { byt[i]=*bytArray; ::MessageBox(NULL,(LPCTSTR)"he","Msg",MB_OK); bytArray++; } byt[i]=0; return S_OK; } Client: IDataProvider* pServer=NULL; HRESULT hr=::CoCreateInstance(CLSID_CDataProvider,NULL,CLSCTX_LOCAL_SERVER,IID_IDataProvider,(LPVOID*)&pServer); short sSize = 5; BYTE* pBytes; //Allocate the shared Memory pBytes = reinterpret_cast(CoTaskMemAlloc(sSize * sizeof(BYTE))); if(pBytes == NULL) return; ::memset(pBytes, 0, sSize); pBytes[0]='K'; pBytes[1]='K'; pBytes[2]='R'; pBytes[3]='D'; pBytes[4]=NULL; pServer->SetData(5,pBytes); CoTaskMemFree(pBytes); //Free the shared Memory
There's many problems here. Firstly you haven't escaped the post properly so it's hard to read. For example:
K ARUN KUMAR wrote:
reinterpret_cast(CoTaskMemAlloc(sSize * sizeof(BYTE)));
You didn't escape the < and > so some important code is missing. You didn't bother to point out explicitly which code is in the client and which is in the server. Surrounding code in <pre> and </pre> tags is generally considered polite, not to mention the fact that it makes the code more readable (uses a fixed width font). You also didn't bother to ensure the code was properly indented. In short, if you want help, put some effort in asking for it (at least as much as I do in replying!). Now onto the code....
K ARUN KUMAR wrote:
BYTE byt[6]; int i=0; for(;*bytArray!=0;i++) { byt[i]=*bytArray; ::MessageBox(NULL,(LPCTSTR)"he","Msg",MB_OK); bytArray++; } byt[i]=0;
This doesn't make use of
size
. Ever heard of buffer overruns? What's with the(LPCTSTR)"he"
? Get rid of the cast. If the string is of the wrong type casting just turns what would have been a compiler error into a runtime error and solves nothing.K ARUN KUMAR wrote:
//Allocate the shared Memory pBytes = reinterpret_cast(CoTaskMemAlloc(sSize * sizeof(BYTE))); if(pBytes == NULL) return;
CoTaskMemAlloc
does ***NOT*** allocate shared memory! You don't need to use it in this case (although there is no harm in using it). Can you tell me more about what actually happens when you run it. Does theCDataProvider::SetData
function get called? Does it display aMessageBox
? How many times?Steve
-
There's many problems here. Firstly you haven't escaped the post properly so it's hard to read. For example:
K ARUN KUMAR wrote:
reinterpret_cast(CoTaskMemAlloc(sSize * sizeof(BYTE)));
You didn't escape the < and > so some important code is missing. You didn't bother to point out explicitly which code is in the client and which is in the server. Surrounding code in <pre> and </pre> tags is generally considered polite, not to mention the fact that it makes the code more readable (uses a fixed width font). You also didn't bother to ensure the code was properly indented. In short, if you want help, put some effort in asking for it (at least as much as I do in replying!). Now onto the code....
K ARUN KUMAR wrote:
BYTE byt[6]; int i=0; for(;*bytArray!=0;i++) { byt[i]=*bytArray; ::MessageBox(NULL,(LPCTSTR)"he","Msg",MB_OK); bytArray++; } byt[i]=0;
This doesn't make use of
size
. Ever heard of buffer overruns? What's with the(LPCTSTR)"he"
? Get rid of the cast. If the string is of the wrong type casting just turns what would have been a compiler error into a runtime error and solves nothing.K ARUN KUMAR wrote:
//Allocate the shared Memory pBytes = reinterpret_cast(CoTaskMemAlloc(sSize * sizeof(BYTE))); if(pBytes == NULL) return;
CoTaskMemAlloc
does ***NOT*** allocate shared memory! You don't need to use it in this case (although there is no harm in using it). Can you tell me more about what actually happens when you run it. Does theCDataProvider::SetData
function get called? Does it display aMessageBox
? How many times?Steve
Sorry Steve. Im new to the message posts. I will follow ur guidelines for sure from next time. Coming to our discussion, iam getting the message box only once, meaning that only one byte is marshalled.
-
Sorry Steve. Im new to the message posts. I will follow ur guidelines for sure from next time. Coming to our discussion, iam getting the message box only once, meaning that only one byte is marshalled.
Post the entire IDL file.
Steve
-
Post the entire IDL file.
Steve
ENTIRE IDL File:
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\prsht.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\mshtml.idl";
import "c:\program files\microsoft visual studio .net 2003\vc7\platformsdk\include\dimm.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\mshtmhst.idl";
import "c:\program files\microsoft visual studio .net 2003\vc7\platformsdk\include\docobj.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\exdisp.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\objsafe.idl";[
object,
uuid(A12EAEC2-2B64-470B-93AF-EFE0855DD3AC),
dual,
helpstring("IDataProvider Interface"),
pointer_default(unique)
]
#line 14 "d:\\vc++\\sample applications\\bytcomp\\dataprovider.h"
interface IDataProvider : IDispatch {
#line 16 "d:\\vc++\\sample applications\\bytcomp\\dataprovider.h"
[id(1),helpstring("method GetSafeArray")] HRESULT GetSafeArray([out,retval] VARIANT *vtSafeArray);
[id(2),helpstring("method SetByteData")] HRESULT SetByteData([in] BYTE *bytArray);
[id(3),helpstring("method SetArray")] HRESULT SetArray([in]BSTR bstrArray);
[id(4),helpstring("method SetThings")] HRESULT SetThings([in]BYTE **ppThings );
[id(5),helpstring("method SetData")] HRESULT SetData([in] ULONG size, [in,size_is(size)] BYTE *pData);
};[ version(1.0), uuid(0381FBF3-32C1-4AF1-B6DD-EFB51815DD72), helpstring("BytComp 1.0 Type Library") ]
library BytComp
{
importlib("stdole2.tlb");
importlib("olepro32.dll");\[ version(1.0), uuid(8816EAAA-91D5-4DCF-BA1A-DD699529D099), helpstring("DataProvider Class") \]
#line 36 "d:\\vc++\\sample applications\\bytcomp\\dataprovider.h"
coclass CDataProvider {
interface IDataProvider;
};}
-
ENTIRE IDL File:
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\prsht.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\mshtml.idl";
import "c:\program files\microsoft visual studio .net 2003\vc7\platformsdk\include\dimm.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\mshtmhst.idl";
import "c:\program files\microsoft visual studio .net 2003\vc7\platformsdk\include\docobj.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\exdisp.idl";
import "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\include\objsafe.idl";[
object,
uuid(A12EAEC2-2B64-470B-93AF-EFE0855DD3AC),
dual,
helpstring("IDataProvider Interface"),
pointer_default(unique)
]
#line 14 "d:\\vc++\\sample applications\\bytcomp\\dataprovider.h"
interface IDataProvider : IDispatch {
#line 16 "d:\\vc++\\sample applications\\bytcomp\\dataprovider.h"
[id(1),helpstring("method GetSafeArray")] HRESULT GetSafeArray([out,retval] VARIANT *vtSafeArray);
[id(2),helpstring("method SetByteData")] HRESULT SetByteData([in] BYTE *bytArray);
[id(3),helpstring("method SetArray")] HRESULT SetArray([in]BSTR bstrArray);
[id(4),helpstring("method SetThings")] HRESULT SetThings([in]BYTE **ppThings );
[id(5),helpstring("method SetData")] HRESULT SetData([in] ULONG size, [in,size_is(size)] BYTE *pData);
};[ version(1.0), uuid(0381FBF3-32C1-4AF1-B6DD-EFB51815DD72), helpstring("BytComp 1.0 Type Library") ]
library BytComp
{
importlib("stdole2.tlb");
importlib("olepro32.dll");\[ version(1.0), uuid(8816EAAA-91D5-4DCF-BA1A-DD699529D099), helpstring("DataProvider Class") \]
#line 36 "d:\\vc++\\sample applications\\bytcomp\\dataprovider.h"
coclass CDataProvider {
interface IDataProvider;
};}
Ok. Open the registry editor and search for the following key:
{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}
If you find if right click on it and select "Export". Post the contents of the file this generates.Steve
-
Ok. Open the registry editor and search for the following key:
{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}
If you find if right click on it and select "Export". Post the contents of the file this generates.Steve
Hi Steve, i found two registry entries.. 1st Entry
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}]
@="IDataProvider"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\NumMethods]
@="12"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid]
@="{00020424-0000-0000-C000-000000000046}"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32]
@="{00020424-0000-0000-C000-000000000046}"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\TypeLib]
@="{0381FBF3-32C1-4AF1-B6DD-EFB51815DD72}"
"Version"="1.0"2nd Registry Entry:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\CLSID\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}]
@="PSFactoryBuffer"[HKEY_CLASSES_ROOT\CLSID\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\InProcServer32]
@="D:\\VC++\\Sample Applications\\BytComp\\DebugPS\\BytCompPS.dll"
"ThreadingModel"="Both" -
Hi Steve, i found two registry entries.. 1st Entry
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}]
@="IDataProvider"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\NumMethods]
@="12"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid]
@="{00020424-0000-0000-C000-000000000046}"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32]
@="{00020424-0000-0000-C000-000000000046}"[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\TypeLib]
@="{0381FBF3-32C1-4AF1-B6DD-EFB51815DD72}"
"Version"="1.0"2nd Registry Entry:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\CLSID\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}]
@="PSFactoryBuffer"[HKEY_CLASSES_ROOT\CLSID\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\InProcServer32]
@="D:\\VC++\\Sample Applications\\BytComp\\DebugPS\\BytCompPS.dll"
"ThreadingModel"="Both"First note that the proxy-stub you built is creatable:
K ARUN KUMAR wrote:
[HKEY_CLASSES_ROOT\CLSID\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\InProcServer32] @="D:\\VC++\\Sample Applications\\BytComp\\DebugPS\\BytCompPS.dll" "ThreadingModel"="Both"
But your interface isn't using it:
K ARUN KUMAR wrote:
[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32] @="{00020424-0000-0000-C000-000000000046}"
{00020424-0000-0000-C000-000000000046}
is the universal marshaller, not your proxy-stub ({A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}
). Also I notice your interface is marked with[dual]
. This means the interface has to be automation compatible and should only use a limited number of types (which doesn't includeBYTE
arrays). Do you get warnings when you compile theIDL
file? Did you ignore them? Since you exported these keys you're free to tinker with them as they can easily be restored using the exported files. Change this key:[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32]
@="{00020424-0000-0000-C000-000000000046}"So that it looks like this:
[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32]
@="{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}"NOTE: Don't alter the exported files, make the changes in the registry. Alternatively copy the exported file, make the change in the copy, then double click the copy.
Steve
-
First note that the proxy-stub you built is creatable:
K ARUN KUMAR wrote:
[HKEY_CLASSES_ROOT\CLSID\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\InProcServer32] @="D:\\VC++\\Sample Applications\\BytComp\\DebugPS\\BytCompPS.dll" "ThreadingModel"="Both"
But your interface isn't using it:
K ARUN KUMAR wrote:
[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32] @="{00020424-0000-0000-C000-000000000046}"
{00020424-0000-0000-C000-000000000046}
is the universal marshaller, not your proxy-stub ({A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}
). Also I notice your interface is marked with[dual]
. This means the interface has to be automation compatible and should only use a limited number of types (which doesn't includeBYTE
arrays). Do you get warnings when you compile theIDL
file? Did you ignore them? Since you exported these keys you're free to tinker with them as they can easily be restored using the exported files. Change this key:[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32]
@="{00020424-0000-0000-C000-000000000046}"So that it looks like this:
[HKEY_CLASSES_ROOT\Interface\{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}\ProxyStubClsid32]
@="{A12EAEC2-2B64-470B-93AF-EFE0855DD3AC}"NOTE: Don't alter the exported files, make the changes in the registry. Alternatively copy the exported file, make the change in the copy, then double click the copy.
Steve
Hi Steve, You are amazing. The change worked. But when iam building the server, it again modifying the registry setting to default Marshaller GUID. How to set my ProxtStub GUID as default?
-
Hi Steve, You are amazing. The change worked. But when iam building the server, it again modifying the registry setting to default Marshaller GUID. How to set my ProxtStub GUID as default?
The problem is your interface is marked as an automation interface but it uses types that are not automation compatible. As I said, I'm sure the MIDL compiler would have warned you about this, and you should always investigate compiler warnings. The are a number of ways to fix this:
- Don't use an automation compatible interface. Remove the
dual
and all the[id]
attributes, and derive fromIUnknown
instead ofIDispatch
. You will not need theIDispatchImp
base class in you server implementation either. - Add another interface to the object (which isn't
dual
and derives fromIUnknown
, as described above) to contain the non-automation compatible methods. - Manually make the changes I described after RegisterTypeLib[^] makes the "wrong" ones. This is a hack though: why use an automation-compatible interface that automation clients will not be able to call?
- Give my posts good marks ;)
Steve
- Don't use an automation compatible interface. Remove the
-
The problem is your interface is marked as an automation interface but it uses types that are not automation compatible. As I said, I'm sure the MIDL compiler would have warned you about this, and you should always investigate compiler warnings. The are a number of ways to fix this:
- Don't use an automation compatible interface. Remove the
dual
and all the[id]
attributes, and derive fromIUnknown
instead ofIDispatch
. You will not need theIDispatchImp
base class in you server implementation either. - Add another interface to the object (which isn't
dual
and derives fromIUnknown
, as described above) to contain the non-automation compatible methods. - Manually make the changes I described after RegisterTypeLib[^] makes the "wrong" ones. This is a hack though: why use an automation-compatible interface that automation clients will not be able to call?
- Give my posts good marks ;)
Steve
Thanks a lot buddy. Sure.. Arun
- Don't use an automation compatible interface. Remove the