std::string WString2String(const std::wstring& str)
{
std::string temp(str.length(), ' ');
std::copy(str.begin(), str.end(), temp.begin());
return temp;
}
Tuesday, April 29, 2008
Tuesday, January 15, 2008
BSTR to String Conversion
#include
std::string myString = _bstr_t (myBSTR);
or
BSTR status= ::SysAllocString(L"Hello World");
CW2A pszConverted (status);
std::string string_key(pszConverted);
std::string myString = _bstr_t (myBSTR);
or
BSTR status= ::SysAllocString(L"Hello World");
CW2A pszConverted (status);
std::string string_key(pszConverted);
Thursday, October 04, 2007
How to expose std::vector in C#
How to expose std::vector in C#
Following Explained is the issue I was facing for COM-.Net Interop:
I have created a COM server in which a collection of vectors need to be exposed to C# sharp client . Each vector contains a array of structures and this vector need to be embedded inside a container, say another vector or list.Since STL classes are not accessible in C#, I used a SAFEARRAY and VARIANT for exposing them. But I am not successful because of the following reason .
1) I am not able to create a VARIANT array of structures.
2) I created a SAFEARRAY of structures and included it in a VARIANT, but when accessed in C# as Object, I am not able to get the structure. Though I am able to typecast it into a Object array , but if accessed in separte gives exception.
3) I am able to access the members of the structure as they are string but not a STRUCTURE as a whole.
4) I tried containing a VARIANT of SAFEARRAY'S in another VARIANT, but it gives a casting error.
5) CComVariant cannot have USD's and VARIANT if used has some problem of Memory Leaks.
Please let me know if you have expertise in COM and C#.Net Interoperability. or Even if you have some alternate solution for this problem?
Solution I found for this problem:
Step 1) Creating a COM Server and expose the vectorAs I have explained in the problem description it is not directly possible to expose std::vector through COM as it std::vector is not a feasible datatype for the IDL file and C Sharp Client.One Approach is to go for a flat file or an XML file where you can dump the vector and access it from the client. I feel that usage of a flat file or an XML file is not a suitable approach. So I selected the following explained approach. I have added demo code to do it, but it is completely for prototyping and none of the coding standards are considered.
1) Add the structure( in my case its all homogenous datatype) elements into a SAFEARRAY of type BSTR.
[code]
.h : CComSafeArray m_structarray;
.cpp :
void CArrayManager::AddStruct(STAGE_STRUCT myStruct)
{
m_structarray.Add(myStruct.myName);
m_structarray.Add(myStruct.myLocation);
[/code]
2) Create another SAFEARRAY of type VARIANT
[code]
.h : CComSafeArray m_sarray;
continuation of previous funtion:
.cpp:
m_sarray.Add(CComVariant(m_structarray));
}
[/code]
in the Interface add another funtion that can retrieve the array in the form of VARIANT
[code]
.idl file:
[id(6), helpstring("method GetStructArray")] HRESULT GetStructArray([out] VARIANT* pvar);
.cpp File :
STDMETHODIMP CArrayManager::GetArray(VARIANT* pvar)
{
CComVariant var(m_sarray);
var.Detach(pvar);
return S_OK;
}
[/code]
Now our COM server is ready with GetArrayFuntion exposing the VARIANT of m_sarray we have created for containing the structures.
SAFEARRAYS could be used directly, but it may lead to memory leaks.
If you are interested you can see this example code (not considered in my explained implementation but i tried using this in another funtion.. it is working fine with IRecordInfo):
[code]
SAFEARRAY *psa;
SAFEARRAYBOUND sab = {2, 0};
MyStruct *pData;
IRecordInfo *pRI;
HRESULT hr;
hr = GetRecordInfoFromGuids(LIBID_AtlSafeArrayLib, 1, 0, 0x409, __uuidof(MyStruct), &pRI);
psa = SafeArrayCreateEx(VT_RECORD, 1, &sab, pRI);
pRI->Release();
pRI = NULL;
hr = SafeArrayAccessData(psa, (void**)&pData);
pData[0].nLongValue = 1;
pData[0].bstrStringValue = SysAllocString(L"First");
pData[1].nLongValue = 2;
pData[1].bstrStringValue = SysAllocString(L"Second");
hr = SafeArrayUnaccessData(psa);
CComVariant var(psa);
var.Detach(pvar);
[/code]
Step 2) In C# Client
[code]
AtlSafeArrayLib.ArrayManagerClass myObj = new AtlSafeArrayLib.ArrayManagerClass();
System.Object myArray;
myObj.GetStructArray(out myArray);
Object[] obj = myArray as Object[];
foreach (Object[] o in obj)
{
string[] strArray = new string[o.Length];
strArray = o as string[];
foreach (string s in strArray)
{
Console.WriteLine(s);
}
}
[/code]
Following Explained is the issue I was facing for COM-.Net Interop:
I have created a COM server in which a collection of vectors need to be exposed to C# sharp client . Each vector contains a array of structures and this vector need to be embedded inside a container, say another vector or list.Since STL classes are not accessible in C#, I used a SAFEARRAY and VARIANT for exposing them. But I am not successful because of the following reason .
1) I am not able to create a VARIANT array of structures.
2) I created a SAFEARRAY of structures and included it in a VARIANT, but when accessed in C# as Object, I am not able to get the structure. Though I am able to typecast it into a Object array , but if accessed in separte gives exception.
3) I am able to access the members of the structure as they are string but not a STRUCTURE as a whole.
4) I tried containing a VARIANT of SAFEARRAY'S in another VARIANT, but it gives a casting error.
5) CComVariant cannot have USD's and VARIANT if used has some problem of Memory Leaks.
Please let me know if you have expertise in COM and C#.Net Interoperability. or Even if you have some alternate solution for this problem?
Solution I found for this problem:
Step 1) Creating a COM Server and expose the vectorAs I have explained in the problem description it is not directly possible to expose std::vector through COM as it std::vector is not a feasible datatype for the IDL file and C Sharp Client.One Approach is to go for a flat file or an XML file where you can dump the vector and access it from the client. I feel that usage of a flat file or an XML file is not a suitable approach. So I selected the following explained approach. I have added demo code to do it, but it is completely for prototyping and none of the coding standards are considered.
1) Add the structure( in my case its all homogenous datatype) elements into a SAFEARRAY of type BSTR.
[code]
.h : CComSafeArray
.cpp :
void CArrayManager::AddStruct(STAGE_STRUCT myStruct)
{
m_structarray.Add(myStruct.myName);
m_structarray.Add(myStruct.myLocation);
[/code]
2) Create another SAFEARRAY of type VARIANT
[code]
.h : CComSafeArray
continuation of previous funtion:
.cpp:
m_sarray.Add(CComVariant(m_structarray));
}
[/code]
in the Interface add another funtion that can retrieve the array in the form of VARIANT
[code]
.idl file:
[id(6), helpstring("method GetStructArray")] HRESULT GetStructArray([out] VARIANT* pvar);
.cpp File :
STDMETHODIMP CArrayManager::GetArray(VARIANT* pvar)
{
CComVariant var(m_sarray);
var.Detach(pvar);
return S_OK;
}
[/code]
Now our COM server is ready with GetArrayFuntion exposing the VARIANT of m_sarray we have created for containing the structures.
SAFEARRAYS could be used directly, but it may lead to memory leaks.
If you are interested you can see this example code (not considered in my explained implementation but i tried using this in another funtion.. it is working fine with IRecordInfo):
[code]
SAFEARRAY *psa;
SAFEARRAYBOUND sab = {2, 0};
MyStruct *pData;
IRecordInfo *pRI;
HRESULT hr;
hr = GetRecordInfoFromGuids(LIBID_AtlSafeArrayLib, 1, 0, 0x409, __uuidof(MyStruct), &pRI);
psa = SafeArrayCreateEx(VT_RECORD, 1, &sab, pRI);
pRI->Release();
pRI = NULL;
hr = SafeArrayAccessData(psa, (void**)&pData);
pData[0].nLongValue = 1;
pData[0].bstrStringValue = SysAllocString(L"First");
pData[1].nLongValue = 2;
pData[1].bstrStringValue = SysAllocString(L"Second");
hr = SafeArrayUnaccessData(psa);
CComVariant var(psa);
var.Detach(pvar);
[/code]
Step 2) In C# Client
[code]
AtlSafeArrayLib.ArrayManagerClass myObj = new AtlSafeArrayLib.ArrayManagerClass();
System.Object myArray;
myObj.GetStructArray(out myArray);
Object[] obj = myArray as Object[];
foreach (Object[] o in obj)
{
string[] strArray = new string[o.Length];
strArray = o as string[];
foreach (string s in strArray)
{
Console.WriteLine(s);
}
}
[/code]
Wednesday, October 03, 2007
Rss - How To embed RSS Feeds To A Reader
Whats Rss?
Rss is the abbrevation of 'Rich Site Summary' or 'Really Simple Syndication'. Its a technology to retrieve information from Websites, blogs. If you are regular reader of website or a blog, this Rss could be useful for you. This will help you to get latest updates of your favorite website directly to your desktop. Since it brings the headline or content of the latest post, it will save your bandwidth too. You can download the content to your mobile since Rss Feeds uses XML for transfering the information. This also help you to integrate this information in your blog or a website.
For eg: When you log into your mail, the new post from JustForUU will be displayed there. When you click on those headlines, you will be able to see it on the blog.
How To Embed JustForUU in Google:
Click Here to Add JustForUU To you Google Home Page :
Step 1) You will be taken to AddToGoogle Page
Step 2)

How To Embed JustForUU in Yahoo:

Rss is the abbrevation of 'Rich Site Summary' or 'Really Simple Syndication'. Its a technology to retrieve information from Websites, blogs. If you are regular reader of website or a blog, this Rss could be useful for you. This will help you to get latest updates of your favorite website directly to your desktop. Since it brings the headline or content of the latest post, it will save your bandwidth too. You can download the content to your mobile since Rss Feeds uses XML for transfering the information. This also help you to integrate this information in your blog or a website.
For eg: When you log into your mail, the new post from JustForUU will be displayed there. When you click on those headlines, you will be able to see it on the blog.
How To Embed JustForUU in Google:
Click Here to Add JustForUU To you Google Home Page :

Step 1) You will be taken to AddToGoogle Page
Step 2)
- If you have Personalized Google Page click on "Add To Google HomePage"
- It will added to your Google Homepage
How To Embed JustForUU in Yahoo:
- Click Here to Add JustForUU To you Yahoo Home Page :
- Loging To yahoo before will make your life simple
- You will be taken to My Yahoo Page
- Click on "Add to My Yahoo" Button -->Done
- Click on the My Yahoo Link to See JustForuu there
To Add JustForUU in Gmail:
- Login to Gmail
- Click on Settings
- In Settings , Click on WebClips
- Give JustForuu Rss Feed URL "http://www.justforuu.blogspot.com/atom.xml" or http://www.justforuu.co.nr/atom.xml in the "SEARCH" box.
- Add the search result.
- Done --> Now See your Gmail, Just above Inbox
If you have any other reader, Please mail to me in justforuu@gmail.com
Wednesday, April 18, 2007
Visual Studio Command Prompt Utilities
Visual Studio: Command Prompt Options
We can do magic using Command prompt while using Visual Studio .Net. I have summarized some utilities in this article. Please feel free to get back to me if you have any doubts or require any further clarification
I know its all of basic level. I have included it as a quickreference for beginner level users.
To Compile a C –Sharp Class:
csc <FileName>.cs
To Create a Library:
csc /t:library <FileName>.cs
To add a dll to GAC (Global Assembly Cache):
gacutil -i <DllName>.dll
Generate StrongName Key :
sn –k <keyname>.snk
To give reference to a dll while running an EXE:
csc \r:<refdllname>.dll <ClientEXEname>.exe
If you want to check the Portable Executable[PE] header:
dumpbin /all /out:<outoutfilename>.txt <appname>.exe
To use Microsoft De Assembler:
ildasm <exename>.exe
To use Microsoft Assembler:
ilasm <Intermediate language file name>.il
To Verify PE file:
peverify <exename>.exe
To view metadata of a file:
Goto Ildasm -> Use Ctrl + M in the
We can do magic using Command prompt while using Visual Studio .Net. I have summarized some utilities in this article. Please feel free to get back to me if you have any doubts or require any further clarification
I know its all of basic level. I have included it as a quickreference for beginner level users.
To Compile a C –Sharp Class:
csc <FileName>.cs
To Create a Library:
csc /t:library <FileName>.cs
To add a dll to GAC (Global Assembly Cache):
gacutil -i <DllName>.dll
Generate StrongName Key :
sn –k <keyname>.snk
To give reference to a dll while running an EXE:
csc \r:<refdllname>.dll <ClientEXEname>.exe
If you want to check the Portable Executable[PE] header:
dumpbin /all /out:<outoutfilename>.txt <appname>.exe
To use Microsoft De Assembler:
ildasm <exename>.exe
To use Microsoft Assembler:
ilasm <Intermediate language file name>.il
To Verify PE file:
peverify <exename>.exe
To view metadata of a file:
Goto Ildasm -> Use Ctrl + M in the
Wednesday, February 21, 2007
New to Mama_Pendse -- Read on : Always your friend
I was just going through some website and find some amazing facts about a Java program which makes magic in internet. Mama_pendse. It is available in Yahoo messenger and Orkut.
Do you remember a small program "Eliza" who talks with you indentifying what ever you type. It is considered to be one among the First AI based application which can identify what you talk and respond accordingly. Mama_pendse is not just a program which identfies what you talk but it works as a utility also. All you have to do is add it on ur buddy list and is always available for you.
When you add it, it welcomes you saying itself a java program which can identify normal english. It provides a help option too. This program is actually created by #Bhargav Pendse who is a software Engineer.He works for Symantec Software. This program seems originated in Pune, India and now it is located in Orlando .FL.
It says "MAMAPENDSE = Artificial Linguistic Internet Computer Entity" and it is eight man years . And most important it says it is a male robot.
You can have normal chat with them, but more importantly it will help you to find lot of information like Railway Chart, word details, google search and lot many more
The help is listed here in detail*:
Command Name tracknumber
Description This will tell you whose phone number is this( USA , pune , mumbai , bangalore ) )Usage : tracknumber , tracknumberusa, tracknumbermumbai, tracknumberbangalore, tracknumberpune
Command Name rail
Description This will tell railway timetable ( only for India )
Usage : rail deccan OR rail shatabdi ;
Command Name horo
Description Horoscope
Usage : horo aries OR horo sagittarius
Command Name explain
Description This is a online Dictionary
Usage : explain beauty OR explain revenge
Command Name findinfo
Description Searches addresses and telephone number of person , Only in US
Usage : findinfo firstname surname
Command Name country
Description Gets Information of any country
Usage : country India
Command Name to_spanish
Description Translate into spanish
Usage : to_spanish Some text in english
Command Name
to_frenchDescription Translate into french
Usage : to_french Some text in english ---And a lot more
Command Name phonespell
Description Convert your phone number into easy to remember words?
Usage : phonespell 1-883-792-8791
Command Name distance
Description find distance between any two cities in the world
Usage : distance mumbai, orlando
Command Name timeDescription Know time in any country
Usage : time usa
Command Name xrateDescription
Gets Exchange rate between two countries
Usage : xrate usa India
Command Name google
Description Search the internet ,(Uses google (TM) api to get search results from internet)Usage : google tajmahal
Command Name climate
Description Gives Climate information of any city in world
Usage : climate orlando , climate mumbai
Command Name quote
Description This will tell you Share Price
Usage : quote VRTS OR quote MSFT
Command Name traffic_usa
Description Know about traffic condition around your area, send me command traffic_usa zipcode
Usage : traffic_usa 10001,traffic_usa 32771
Command Name comment
Description Let me know how I am doing , please send me your comments about meUsage : comment "your comment goes here"
I said him that I am leaving now :) and his response is byebye..!.Its real fun and dont forget to try this once.
# information recieved from mama_pendse. * help extracted from Mama_Pendse
Do you remember a small program "Eliza" who talks with you indentifying what ever you type. It is considered to be one among the First AI based application which can identify what you talk and respond accordingly. Mama_pendse is not just a program which identfies what you talk but it works as a utility also. All you have to do is add it on ur buddy list and is always available for you.
When you add it, it welcomes you saying itself a java program which can identify normal english. It provides a help option too. This program is actually created by #Bhargav Pendse who is a software Engineer.He works for Symantec Software. This program seems originated in Pune, India and now it is located in Orlando .FL.
It says "MAMAPENDSE = Artificial Linguistic Internet Computer Entity" and it is eight man years . And most important it says it is a male robot.
You can have normal chat with them, but more importantly it will help you to find lot of information like Railway Chart, word details, google search and lot many more
The help is listed here in detail*:
Command Name tracknumber
Description This will tell you whose phone number is this( USA , pune , mumbai , bangalore ) )Usage : tracknumber , tracknumberusa, tracknumbermumbai, tracknumberbangalore, tracknumberpune
Command Name rail
Description This will tell railway timetable ( only for India )
Usage : rail deccan OR rail shatabdi ;
Command Name horo
Description Horoscope
Usage : horo aries OR horo sagittarius
Command Name explain
Description This is a online Dictionary
Usage : explain beauty OR explain revenge
Command Name findinfo
Description Searches addresses and telephone number of person , Only in US
Usage : findinfo firstname surname
Command Name country
Description Gets Information of any country
Usage : country India
Command Name to_spanish
Description Translate into spanish
Usage : to_spanish Some text in english
Command Name
to_frenchDescription Translate into french
Usage : to_french Some text in english ---And a lot more
Command Name phonespell
Description Convert your phone number into easy to remember words?
Usage : phonespell 1-883-792-8791
Command Name distance
Description find distance between any two cities in the world
Usage : distance mumbai, orlando
Command Name timeDescription Know time in any country
Usage : time usa
Command Name xrateDescription
Gets Exchange rate between two countries
Usage : xrate usa India
Command Name google
Description Search the internet ,(Uses google (TM) api to get search results from internet)Usage : google tajmahal
Command Name climate
Description Gives Climate information of any city in world
Usage : climate orlando , climate mumbai
Command Name quote
Description This will tell you Share Price
Usage : quote VRTS OR quote MSFT
Command Name traffic_usa
Description Know about traffic condition around your area, send me command traffic_usa zipcode
Usage : traffic_usa 10001,traffic_usa 32771
Command Name comment
Description Let me know how I am doing , please send me your comments about meUsage : comment "your comment goes here"
I said him that I am leaving now :) and his response is byebye..!.Its real fun and dont forget to try this once.
# information recieved from mama_pendse. * help extracted from Mama_Pendse
Monday, February 19, 2007
Earn Money through Adsense
If you own a personal page or if you have a blog and wish to get revenue for the effort you take , then go ahead and read this post.
For a blogger it’s really difficult to get advertisements in his/her site from outside. Google provides a very easy way of earning money from your blogs. Consider your site/blog fetches at least 20-30 visitors per day. They have come to your site because he or she is interested in your content.
Once you register your site to Google adsense, adsense crawler will identify relevant ads for your sites and displays it on your site.
For eg: Visitor 'X' visited your site for getting knowledge about your hometown. And when he find Google adsense displaying all corresponding ads regarding your hometown* or other interesting features presented in your site its quiet normal tendency that he/she will check out those relevant sites also. This is the advantage of using google sense.
For each clicks your visitor makes to your ads will be tracked by google adsense.
For eg: Google adsense will count number of page impressions, total number of times your pages are viewed. For each click the information will be updated and your account will be updated with the money you have received for each click. When it reaches a total 100$, the amount will be send to you.
To register in Google adsense follow these simple instructions:
Click on picture "Earn money by showing relevant ads" above the cricket scores in this page. It will take you to simple registration process.
In case you have any doubts you can check help on Google or write to me with your query.
So what are you waiting for... Add adsense to your page and start getting revenue from your simple effort.
* Content elements should be listed in Google. #GOOGLE is a trademark of Google Inc.
For a blogger it’s really difficult to get advertisements in his/her site from outside. Google provides a very easy way of earning money from your blogs. Consider your site/blog fetches at least 20-30 visitors per day. They have come to your site because he or she is interested in your content.
Once you register your site to Google adsense, adsense crawler will identify relevant ads for your sites and displays it on your site.
For eg: Visitor 'X' visited your site for getting knowledge about your hometown. And when he find Google adsense displaying all corresponding ads regarding your hometown* or other interesting features presented in your site its quiet normal tendency that he/she will check out those relevant sites also. This is the advantage of using google sense.
For each clicks your visitor makes to your ads will be tracked by google adsense.
For eg: Google adsense will count number of page impressions, total number of times your pages are viewed. For each click the information will be updated and your account will be updated with the money you have received for each click. When it reaches a total 100$, the amount will be send to you.
To register in Google adsense follow these simple instructions:
Click on picture "Earn money by showing relevant ads" above the cricket scores in this page. It will take you to simple registration process.
In case you have any doubts you can check help on Google or write to me with your query.
So what are you waiting for... Add adsense to your page and start getting revenue from your simple effort.
* Content elements should be listed in Google. #GOOGLE is a trademark of Google Inc.
Subscribe to:
Posts (Atom)