Click here to Skip to main content
11,926,961 members (54,662 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ string

I am trying to develop a database migration utility.
I am using following function to create DSN.

SQLConfigDataSource(NULL, ODBC_ADD_DSN,"SQL Server",ss)

Last parameter of this fucntion is a connection string. It is of data type LPCSTR.
This function expects NULL character as a seperator.

In short it expects the string in following format.

If you notice there are NULL characters (\0) in between. Due to this i am not able to create this string dynamically as strcat function cannot concatenate the string with NULL characters, It skips the string beyond NULL(which is i think the correct behavior).

Help of any sort in this regard will be highly appreciated.
Posted 25-May-11 20:47pm
Kozlov_Sergey 26-May-11 2:24am
You should known strings length to do this.
To get length, answer following question:
"What is the sign of the end of the line?"
It seems to me, here it is '\0\0'.
Nithin Sundar 26-May-11 2:28am
You do not need length or anything to achieve this. See my solution. A simple concatenation is enough for this.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I had the same problem. You can do something like this.

This is for MFC.

CString sAttributes;
//Form the attributes string
sAttributes += "DSN=";
sAttributes += YOUR DSN NAME;
sAttributes += '\0';
sAttributes += "Server=";
sAttributes += SERVER NAME;
sAttributes += '\0';

You can do the same for extra attributes.
SP 24 26-May-11 2:45am
It is necessary?? I checked one link "" and without appending a null character it is working.
Please explain.
Thanks in advance.
Nithin Sundar 26-May-11 2:48am
Actually from personal experience, it did not work for me till I appended the null character. From what I read on MSDN the SQLConfigDataSource function expects the attribute string to have that null character.

I appended the null character using this and I was able to get it working. If it works for you then great!
PrafullaVedante 26-May-11 2:53am
It do not work without NULL character.
Nithin Sundar 26-May-11 2:56am
Yup that's what I was telling him. It didn't work for me without the null character as well. If he could make it work, then it's good.
SP 24 26-May-11 3:00am
+ 5 for you answer :)
SP 24 26-May-11 2:59am
See the OP response:
here is the working one, an adaptation of GCDEF code, a lot of thanks for you!! :D :D :D

I am not sure about it and that's why I ask this question to Nithin.
SP 24 26-May-11 2:57am
No I am not trying it. I just read the url that I give you and here the OP tells that it is working.
I am confused so I asked you about this.
Thanks for your reply.
Nithin Sundar 26-May-11 3:01am
Okay I'm confused now. Do you mean the OP on the codeguru forum or do you mean the OP who posted this question?
SP 24 26-May-11 3:05am
OP on the Codeguru forum.

See the OP response: here is the working one, an adaptation of GCDEF code, a lot of thanks for you!! :D :D :D
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

struct testfunctor
void operator()(char& c) { if(c == '|') c = '\0'; }
std::for_each( str.begin(), str.end(), testfunctor() );
This should work..
PrafullaVedante 26-May-11 2:54am
Thanks a lot. It works.
Nithin Sundar 26-May-11 2:56am
My 5!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Isn't it possible to copy it yourself using a pointer to the string and then copy every character in it and to stop when you receive two NULL characters in a row?
Nithin Sundar 26-May-11 2:27am
He needs to form that string dynamically with the '\0' inside it to pass it as a parameter to that function. He does not want to stop when he gets null characters. He needs those null characters to be present inside the string.

I might be wrong in understanding your possible solution so feel free to correct me.
PrafullaVedante 26-May-11 2:38am
You got it perfect :)
Nithin Sundar 26-May-11 2:41am
Do try my solution and let me know if it worked for you. My solution is for MFC as of now. If you need it for Win32 you can try using LPSTR or so.
Patrick Kalkman 26-May-11 2:34am
Yes that was what I meant, just copy the characters until you receive two null characters after each other.
Nithin Sundar 26-May-11 2:42am
Okay that makes sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 26 May 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100