Solved

SysFreeString & non-NULL starting pointer

Posted on 2003-11-15
17
1,212 Views
Last Modified: 2012-06-21
// 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)
0
Comment
Question by:G00fy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9755305
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
 
LVL 15

Expert Comment

by:dualsoul
ID: 9755312
as i understand you want to know if you have empty string?
try this:

if(waarin==NULL || SysStringLen(waarin)==0){
//empty string
}
0
 

Author Comment

by:G00fy
ID: 9756772
---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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:DanRollins
ID: 9757864
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
 

Author Comment

by:G00fy
ID: 9757902
Yeah I know, but _bstr_t is not a type you can use in MIDL
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 9757920
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
 

Author Comment

by:G00fy
ID: 9757925
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 9757948
>> 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
 

Author Comment

by:G00fy
ID: 9757976
// 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
 
LVL 49

Expert Comment

by:DanRollins
ID: 9760226
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 9784103
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
 

Author Comment

by:G00fy
ID: 9788958
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
 
LVL 15

Expert Comment

by:dualsoul
ID: 9790177
>this function created a new LabelInformation_t-structure, with setting all the BSTR's to NULL...
  this exactly what DanRollins said to you.
0
 

Author Comment

by:G00fy
ID: 9790383
Not exactly ... But you got a point there...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9790975
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
 

Author Comment

by:G00fy
ID: 9792222
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 9792287
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question