SysFreeString & non-NULL starting pointer

// code
BSTR ConvertStringToBSTR_( BSTR &waarin, const TCHAR* pSrc )
{
  if (waarin)
  {
    ::SysFreeString(waarin);
    waarin = NULL;
  }
\\ code

as you can see, I pass in a BSTR... This will need to be freed & reallocated with the one I want... But the first time I encounter this function, my waarin-pointer = 0xcccccccc ... Now I do read somewhere:

0xCC, 0xCCCCCCCC - The /GX Microsoft Visual C++ compiler option initialises all local variables not explicitly initialised by the program. It fills all memory used by these variables with 0xCC, 0xCCCCCCCC.


How can I circumvent this problem? And it is NOT possible to initialize those strings :S (they're defined in a structure which resides inside IDL)
G00fyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You cannot 'SysFreeString()' with a reference to a BSTR (furthermore, a reference will never be NULL) - use

BSTR ConvertStringToBSTR_( BSTR*& pwaarin, const TCHAR* pSrc )
{
 if (pwaarin)
 {
   ::SysFreeString(*pwaarin);
   pwaarin = NULL;
 }


0
dualsoulCommented:
as i understand you want to know if you have empty string?
try this:

if(waarin==NULL || SysStringLen(waarin)==0){
//empty string
}
0
G00fyAuthor Commented:
---jkr---
pInvoer = 0x0012f760
 +--> 0xcccccccc ""

so pInvoer (or pWaarin) is defined, but the SysFreeString still crashes because the BSTR hasn't been initted yet :S


---dualsoul---
waarin == NULL <-- ok, if it's initted yes.
SysStringLen <-- same error... The BSTR ain't initted yet ... In debug mode it is initted to 0xcccccccc, but I can't check on that because it will not be that value in release build...



Any thoughts?
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

DanRollinsCommented:
I suggest using _bstr_t objects.  
They take care of all conversions easily and they allocate and release the string like one would expect from a normal C++ object... ie, when going out of scope or with delete (after new).

-- Dan
0
G00fyAuthor Commented:
Yeah I know, but _bstr_t is not a type you can use in MIDL
0
DanRollinsCommented:
You can use it transparently in your code that implements the MIDL.

[propget, id(2), helpstring("property SomeValue")] HRESULT SomeValue([in]BSTR sz1, [in]BSTR sz2, [out, retval] int *pVal);


STDMETHODIMP CMyObject::get_SomeValue(BSTR szMsg, BSTR szTitle, int *pVal)
{
      _bstr_t s1(szMsg);
      _bstr_t s2(szTitle);
      
      *pVal= strlen(s1);
      ::MessageBox(0,s1,s2, MB_OK );
      return S_OK;
}

You can seamlessly convert to and from 8-bit strings andUNICODE using the _bstr_t conversion operators:

_bstr_t s1( L"A UNICODE string" );
char* p= (char *)s1;

char* sz8= "a good old-fashioned string, like God intended";
BSTR bs=   (BSTR)_bstr_t s1( sz8  );

I tell you, it takes all of the sting out of BSTR handling.

-- Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
G00fyAuthor Commented:
Yeah yeah... I know :) I'm not that unknown to C++ programming u know :)

The problem is I've a structure in MIDL which I need to initialize. In normal code ok, I should make a constructor ... But in this case that ain't an option...
0
DanRollinsCommented:
>> I'm not that unknown to C++ programming u know
How would I know that?

Anyway, you lost me.  If it is an object, why can't you implement a constructor?  I thought this question was about BSTRs.  Maybe if you post some MIDL code, I can understand the question better.

-- Dan
0
G00fyAuthor Commented:
// code
// Contact Information
[
  uuid(6F7EA100-11F7-4250-A753-DBFC3D5FEB57),
  helpstring("Contact Information")
]
typedef struct ContactInformation_t {
  [helpstring("Name 1")     ]    BSTR           Name;
\\code

and a constructor cannot be made in MIDL... That's the problem ;)
0
DanRollinsCommented:
struct
simply describes what's in the structure... just as in a C struct.   I woudn't expect IDL to provide a means to preset its content.  AFAICS, it defines, but does not declare.  I would expect the caller to intialize a chunk of memory and would use the structure as a template. So again, I don't get it.

-- Dan
0
DanRollinsCommented:
So, how's it going?  
I provided a step-by-step way to get pre-set data from a DLLin your other question, as well.  Did you check  http:/Cplusplus/Q_20793171.html

-- Dan
0
G00fyAuthor Commented:
This issue has been solved.

I couldn't get it to work using the way you tried to describe.

I did it like this:

// code
LabelInformation_t * function();
  this function created a new LabelInformation_t-structure, with setting all the BSTR's to NULL...
void function( LabelInformation ** );
  this function destroys the data again ...

So that's how I do it ... ;)

I've got to decide how to give points on this matter because it's not really the answer to my question...
0
dualsoulCommented:
>this function created a new LabelInformation_t-structure, with setting all the BSTR's to NULL...
  this exactly what DanRollins said to you.
0
G00fyAuthor Commented:
Not exactly ... But you got a point there...
0
DanRollinsCommented:
Next time, I'll work really really hard on your problems because I so enjoy getting Cs.  For instance, if I work on one of your problems and then two others and then one of your problems and then two others, my gradding history witll be CAACAA.
0
G00fyAuthor Commented:
I give points as I see fit. I do not give A's when the answer didn't really helped me.

as you might have noticed is that i still gave you the experience points hah?
0
DanRollinsCommented:
Please see the grading guidelines.  http://www.experts-exchange.com/help.jsp#hi73
Most Experts would rather get no points at all than to be insulted with a C.  Just a heads up.
-- Dan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.