Search This Blog

Tuesday, September 22, 2009

Calling MFC C++ API from C# (e.g. CString& param)

I have a C++ dll that written via MFC, I exposed an API from my dll, for this sample :
HRESULT __declspec( dllexport )RunTest(const CString& strIn, CString& strOut);

now i want to call it from my c# code, so i tried the next decalrtion:
[DllImport("myDLL.dll", CharSet = CharSet.Ansi)]
public static extern int RunTest(ref StringBuilder strIn, ref StringBuilder strOut);
the strings arrived to the c++ dlls , the problem was getting some unexpcted assertion in destructor and operator= of Cstring , any way as you know CString is a Class with buffer in it , and its looks like the marshal didn’t get well in runtime , I get the string but I get crashed from time to time.
so my workaround was as follow , in the C++ dll i created a new API method (wrapper one):

HRESULT __declspec( dllexport )RunTestManaged(LPCTSTR strXmlIn, LPTSTR strXmlOut, int buffCount)
//--------------------------------------------------------------------------
{
CString param1;
RunTest(strXmlIn,param1);
strncpy(strXmlOut,param1,buffCount);
return 0;
}

and in the C# code i created the next import section:
[DllImport("myDLL.dll", CharSet = CharSet.Ansi)]
public static extern int RunTestManaged(String strIn, ref StringBuilder strOut,int strOutBuffCount);


Make sure to allocate enough buffer in the string builder, this way the strIn and the strOut will be transferred perfectly.

Yaniv T

using MFC dll's on Azure

Hi
I read somewhere that you can not run MFC dlls in the Azure platform , so its is wrong!!!
i managed to use mfc dll that i developed on .... VC++ 6.0 . in the Azure .
Be aware i didnt test MFC dll that developed on visualstudio 2005/2008.
how did i call my 32bit mfc dll from the web_role that runs in 64 bit ???
i created a WCF self host that using named pipe (net.pipe) , and when the web role loaded i run this self host , and from the self host i call to my native library ,via dllimport , the regular way.

When i deploy my package i didn't add any extra dll , its looks like the instance already have the needed library to support my mfc dll.

more than that my dll connect to sql server via odbc(CDatabase, CRecordSet) , so i just needed to change my connection string to point to sql azure ,and boooom i managed to make it work in the Azure too , again no extra dll needed to the package.

some useful links :
Using a 32bit Native DLL in Windows Azure
Executing Native Code in Windows Azure and the Development Environment
MSDN FullTrust Sample

i hope its save you some test time.
Yaniv

Monday, September 21, 2009

Show directory size in UNIX

the next line works perfectly under AIX.
du -gs /var/* | sort

the results will be the directory size sorted under the /var directory.

YanivT

Monday, September 7, 2009

Connect to SQL Azure - starter links

Recently i am dealing with SQL azura , a relational db on the cloud from Microsoft.
here are some starter links to put you in the map:
SQL Azure Database - HOME .
Microsoft SQL Azure FAQ.
you will have to create your account for SQL Azure (CTP this days), after you will get your code you can manage the DB's.
for a complete set of action how to connect to your DB from your preferred tools , i found the next post First Impressions with the New SQL Azure that will give you a good starting way to interact with your cloud db the
Connecting to SQL Azure from SQL Management Studio 2008 can help too if needed.
NOTES:
if you are in the office and you have a firewall , you will have to open it for:
Access type: outbound
Destination port: 1433
Destination IP range: 65.55.*.*

In addition, if your network has a proxy server for accessing the Internet then you need to configure your proxy server to allow outbound 1433 connections and you need to configure your system to use the proxy server.
this from here Firewall Issues.

hope its help you to start your cloud db - i hope its works well.
and of course MSDN link - SQL Azure .
another good blog The latest news and insight from the SQL Azure team.

enjoy
Yaniv