Solved

SysFreeString & non-NULL starting pointer

Posted on 2003-11-15
17
1,163 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now