Ulhas Tuscano
Ulhas Tuscano

Reputation: 5620

How do i convert a C++ function in c# function

I am working on Amibroker C# plugin project. Amibroker SDK is written in C++ but, I am using a C# plugin which does exactly what Amibroker C++ does C# Plugin link

Everything in C# plugin works just fine except one function which is written in c++ this way:

PLUGINAPI struct RecentInfo* GetRecentInfo(LPCTSTR ticker)
{
   //Process & return RecentInfo* (RecentInfo is a structure)
}

In C# standard plugin it has been converted this way

public RecentInfo GetRecentInfo(string ticker)
{
    //Process & Return RecentInfo
}

Unfortunately, Amibroker application crashes with this wrong conversion. So I did try to convert it my way to get Amibroker App working, but failed multiple times This is what i have tried so far:

Attemp 1:

unsafe public RecentInfo* GetRecentInfo(string ticker)
{
    //Process & Return RecentInfo* (RecentInfo structure is declared as unsafe)
}

Impact:

Amibroker app does not load

Attemp 2:

public IntPtr GetRecentInfo(string ticker)
{
    //Process & Return Pointer using Marshal.StructureToPtr
}

Impact:

Amibroker app does not load

Attemp 3:

public void GetRecentInfo(string ticker)
{
    //Useless becoz no return type
}

Impact:

Amibroker loads & does call function correctly but how do return a structure pointer

So, I am scratching my head to find out the exact conversion of C++ function in C#

Upvotes: 3

Views: 340

Answers (1)

qwr
qwr

Reputation: 3670

If it is fully written in c# then , this is pretty well ,think there is problem in implementation not in calling

public RecentInfo GetRecentInfo(string ticker)
{
      RecentInfo rc;
    //Process & Return RecentInfo
      return rc;
}

or this , (you can use ref too)

public void GetRecentInfo(string ticker,out RecentInfo rc )
{
rc=new RecentInfo();
....process

 return ;
}

Upvotes: 2

Related Questions