Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
Hi,

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.
"ExpTempSQL\0SERVER=(local)\0Trusted_Connection=yes\0\0"

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
Comments
[no name] 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.

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.
 
Share this answer
 
Comments
ShilpiP 26-May-11 2:45am    
It is necessary?? I checked one link "http://www.codeguru.com/forum/archive/index.php/t-274643.html" 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.
ShilpiP 26-May-11 3:00am    
+ 5 for you answer :)
struct testfunctor
{
void operator()(char& c) { if(c == '|') c = '\0'; }
};
std::for_each( str.begin(), str.end(), testfunctor() );

This should work..
 
Share this answer
 
Comments
PrafullaVedante 26-May-11 2:54am    
Thanks a lot. It works.
Nithin Sundar 26-May-11 2:56am    
My 5!
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?
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900