how to use char* in ATL
-
I am learning Program COM by ATL,I build a inpro-server dll and add a com object ComputerInfo,I only add a method to IComputerInfo and not to any thing just as bellow: interface IComputerInfo : IDispatch { [id(1), helpstring("method Test")] HRESULT GetCPU(char* bufName,const int* size); }; STDMETHODIMP CComputerInfo::GetCPU(char* bufName,const int* size) { // TODO: Add your implementation code here return S_OK; } Compiler shows: ATLFirst.cpp error C2259: 'CComObject' : cannot instantiate abstract class due to following members: atlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void *,const struct _GUID &,void ** )' f:\microsoft visual studio\vc98\atl\include\atlcom.h(1827) : warning C4259: 'long __stdcall IComputerInfo::GetCPU(unsigned char *,const int *)' : pure virtual function was not defined if I have change the metho to GetCPU(unsigned char* bufName,const int* size) all right,it works well,why???
-
I am learning Program COM by ATL,I build a inpro-server dll and add a com object ComputerInfo,I only add a method to IComputerInfo and not to any thing just as bellow: interface IComputerInfo : IDispatch { [id(1), helpstring("method Test")] HRESULT GetCPU(char* bufName,const int* size); }; STDMETHODIMP CComputerInfo::GetCPU(char* bufName,const int* size) { // TODO: Add your implementation code here return S_OK; } Compiler shows: ATLFirst.cpp error C2259: 'CComObject' : cannot instantiate abstract class due to following members: atlcom.h(1823) : while compiling class-template member function 'long __stdcall ATL::CComCreator >::CreateInstance(void *,const struct _GUID &,void ** )' f:\microsoft visual studio\vc98\atl\include\atlcom.h(1827) : warning C4259: 'long __stdcall IComputerInfo::GetCPU(unsigned char *,const int *)' : pure virtual function was not defined if I have change the metho to GetCPU(unsigned char* bufName,const int* size) all right,it works well,why???
I think this is a problem (bug?) in the IDL compiler, that generated the
unsigned char*
signature instead thechar*
Anyway, if you want to be able to call your component from e.g. Visual Basic or something, it's better to useBSTR
type for strings. This is basically counted unicode string (Basic STRing). Then you can cope with that using helper classesCComBSTR
from ATL or_bstr_t
fromcomdef.h
Hope this helps -
I think this is a problem (bug?) in the IDL compiler, that generated the
unsigned char*
signature instead thechar*
Anyway, if you want to be able to call your component from e.g. Visual Basic or something, it's better to useBSTR
type for strings. This is basically counted unicode string (Basic STRing). Then you can cope with that using helper classesCComBSTR
from ATL or_bstr_t
fromcomdef.h
Hope this helpsthanks a lot.and I also have a question. unsigned char buf[]="hello";//OK strcpy(buf,"hello");//Error:because buf is not char* and if I forced buf to char* and use MessageBox to show it,I found many rubbish code.I don't know the advantage of the unsigned char*,could you tell me?
-
thanks a lot.and I also have a question. unsigned char buf[]="hello";//OK strcpy(buf,"hello");//Error:because buf is not char* and if I forced buf to char* and use MessageBox to show it,I found many rubbish code.I don't know the advantage of the unsigned char*,could you tell me?
unsigned char
is in equal toBYTE
type from windows header. Difference between these is stated in the name of the type - both are 1 byte of size, butchar
is signed andunsigned char
is... unsigned (unless you switch the C++ compiler to treatchar
as unsigned). because in C++ the type checking is quite strict, you cannot implicitly convert unsigned to signed and vice versa. You have to state explicitly, that you really want to do it. (e.g. by casting) Just an advice - don't use the strcpy and similar functions. Find and stick to some string wrapper - be itstd::string
from stl libraries orCString
from MFC/ATL. This would save you lot of troubles when looking for bugs inside string manipulation (forgotten ending \0, buffer overruns or whatever)