Login Page - Create Account

Support Board


Date/Time: Fri, 03 May 2024 07:25:49 +0000



C# code integration

View Count: 2726

[2017-07-28 06:55:11]
User921987 - Posts: 234
I have some code in C# which generates some information (double values, arrays and text strings) I would like to bring into SierraChart (ACSIL). What method is a right way to do this ?

For example do I have to put my C# code into DLL or be runned in service/pure application and then use some interprocess communication. What do you suggest ?
Date Time Of Last Edit: 2017-07-29 13:18:54
[2017-07-28 12:23:26]
Sierra Chart Engineering - Posts: 104368
You probably would want to create a mixed managed and unmanaged DLL and have it export functions that are unmanaged and call those from the ACSIL DLL.

Assuming that the exported unmanaged function can call a managed function.

C# is not our area of expertise.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-07-28 12:23:46
[2017-07-28 12:42:17]
User921987 - Posts: 234
Ok I'll try to convert C# into a DLL which can be called from C++ code. Sounds like this is a way to go.
[2017-07-29 08:51:33]
gomifromparis - Posts: 244
You can call your c# lib from the Sierra dll if you compile it as a C++/CLI dll, it will then be able to run native and c# code.

Here's what it will look like :

SCSFExport scsf_GomSR(SCStudyGraphRef sc)
{
  if (sc.SetDefaults)
  {

    sc.GraphName = "GomSR";
    sc.StudyDescription = "Gom SR Zones";
    sc.AutoLoop = 1;

    Settings^ settings = gcnew Settings();
    settings->Show = false;

    return;
  }
}

Settings^ settings = gcnew Settings(); instantiates a c# object from the c++ code
settings->Show = false;; uses it.
[2017-07-29 13:31:42]
User921987 - Posts: 234
Thanks. This was useful information.
[2017-07-31 19:46:25]
DabbaDo - Posts: 146
@gomifromparis,

Your answer was VERY helpful. I'm able to call a C# method now, even a WinForm, from a study.
My only problem is that the C# dlls must be in C:\SierraChart, not C:\SierraChart\Data, elser SierraChart gets eefileloadexception.
I haven't figured out a way to register a ResolveEventHandler.
Do you have any advice? If nothing else, I can just build the C# dlls to C:\SierraChart.

Cheers,
Dale
[2017-07-31 20:10:48]
gomifromparis - Posts: 244
No, I haven't found a way to overcome that, so I deploy the c# in sierrachart and the dll in sierrachart\data.
[2017-07-31 20:46:56]
Sierra Chart Engineering - Posts: 104368
If it would help, we could support a different folder for DLL files.

But we do not want them in the main Sierra Chart folder. But then again you could specify that as the folder.


The default will be the Data subfolder.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-07-31 21:19:31]
DabbaDo - Posts: 146
The problem is that the c++/CLR dll (which is in the Data subfolder) ONLY looks in the executable directory (the main Sierra Chart folder) for any c# dlls. When it doesn't find them there, it throws you the eefileloadexception.
IMHO the "proper" way to handle this would be with a ResolveEventHandler like the one below (copied from http://www.cplusplus.com/forum/windows/196600/. But SC would have to give us a hook that was called once, and preferably only once, during start-up (or at least sometime you call the study) so we could hook up the event.


using namespace System;

public ref class AssemblyResolver{
public:
static System::Reflection::Assembly ^ResolveEventHandler(System::Object ^sender, System::ResolveEventArgs ^args){
auto sb = gcnew System::Text::StringBuilder();
sb->Append(GetAssemblyDirectory());
const char * const name = "\\required_assembly.dll";
for (auto p = name; *p; p++)
sb->Append((Char)*p);
return System::Reflection::Assembly::LoadFrom(sb->ToString());
}

//Returns the containing directory of the assembly that contains the function.
static String ^GetAssemblyDirectory(){
auto codeBase = System::Reflection::Assembly::GetExecutingAssembly()->CodeBase;
auto uri = gcnew UriBuilder(codeBase);
auto path = Uri::UnescapeDataString(uri->Path);
return System::IO::Path::GetDirectoryName(path);
}
};

API Instance *initialize(){
auto domain = System::AppDomain::CurrentDomain;
domain->AssemblyResolve += gcnew System::ResolveEventHandler(AssemblyResolver::ResolveEventHandler);

//(Application-specific stuff)
}

[2017-07-31 23:34:37]
Sierra Chart Engineering - Posts: 104368
Sierra Chart is an unmanaged program and has no ability to support code like the above.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-08-01 07:43:33]
gomifromparis - Posts: 244
You can also link the c# lib into the dll using a netmodule.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account