How to run a function from a .SO file in C#? - c#

This is my code.
public static String telegramsetime(String str, String str2)
{
try
{
string text = telegramsettime(str, str2);
return text;
}
catch (Exception th)
{
return th.ToString();
}
}
The "telegramsettime" is a function which is inside a file named "libjnitg.so".How can i run this function properly?

Related

How to unify and manage overloaded functions?

This code works by taking a function as a parameter and using try-catch to log the result of the function.
When I change the functionDecorator here, I want the changes to be applied automatically to functionDecorator<T> or functionDecorator<T1, T2> without copying the entire code as it is now
These functions that do almost the same thing Is there a way to manage each other's behavior in one place, including even try-catch statements?
public void funtionDecorator(Func<bool> Func, string successText = "success", string failText = "fail", string errorText = "error")
{
try
{
if (Func())
{
txtStatusBar.Text = successText;
}
else
{
txtStatusBar.Text = failText;
}
}
catch(Exception ex)
{
txtStatusBar.Text = errorText + Func.Method.Name + ex.ToString();
}
}
// ex : funtionDecorator<DataTable>(useDataFuntion, dt);
public void funtionDecorator<T>(Func<T, bool> Func, T type, string successText = "success", string failText = "fail", string errorText = "error")
{
try
{
if (Func(type))
{
txtStatusBar.Text = successText;
}
else
{
txtStatusBar.Text = failText;
}
}
catch (Exception ex)
{
txtStatusBar.Text = errorText + Func.Method.Name + ex.ToString();
}
}
// ex : funtionDecorator<DataTable, Double>(useDataFuntion, dt, value);
public void funtionDecorator<T1, T2>(Func<T1, T2, bool> Func, T1 type1, T2 type2, string successText = "success", string failText = "fail", string errorText = "error")
{
try
{
if (Func(type1, type2))
{
txtStatusBar.Text = successText;
}
else
{
txtStatusBar.Text = failText;
}
}
catch (Exception ex)
{
txtStatusBar.Text = errorText + Func.Method.Name + ex.ToString();
}
}
I tried to handle it dynamically using dynamic, but the function type was not converted to dynamic, so I couldn't find a way.

Convert VB set/get function to c# function

I'm trying to convert following vb set/get function to c#. It is used through an ActiveX page like :
Item.CtxString(document.getElementById("setvar").value+"1")=document.getElementById("setval").value;
or :
Item.CtxString("var1") = "var";
The following code is used in VB.NET :
Public Property CtxString(ByVal strItemType As String) As String
Get
Try
Return myContext.ContextString(strItemType)
Catch ex As Exception
Return ""
End Try
End Get
Set(ByVal value As String)
Try
myContext.ContextString(strItemType) = value
Catch ex As Exception
End Try
End Set
End Property
Public Sub SetCtxString(ByVal strItemType As String, ByVal value As String)
Try
myContext.ContextString(strItemType) = value
Catch ex As Exception
End Try
End Sub
I'm trying to convert this from VB to C#, with following function :
public string CtxString
{
get
{
return ctxString;
}
set
{
ctxString = value;
}
}
public void SetCtxString(string value)
{
this.ctxString = value;
}
ContextString is a function used in c++, which needs to be converted to c# aswell..
STDMETHODIMP CContextATL::get_ContextString(BSTR strItemType, BSTR *pVal)
{
try
{
_bstr_t strItem(strItemType, true);
_bstr_t strTemp;
char szBuffer[2048] = {0};
CContextItem *pItem = _Module.GetContextItemFromEnvironment(m_strEnv, (char *)strItem);
if(pItem != NULL)
{
strTemp = pItem->GetContextStringValue().c_str();
*pVal = ::SysAllocString(static_cast<const wchar_t*>(strTemp));
sprintf( szBuffer, "ContextString Key = '%s' Value = '%s' read by Client %s with name = %s in Environment %s\r\n", (char *)strItem, (char *)strTemp, m_strId.c_str(), m_strClientName.c_str(), m_strEnv.c_str());
}
else
{
sprintf( szBuffer, "ContextString Key = '%s' not found while reading by Client %s with name = %s in Environment %s\r\n", (char *)strItem, m_strId.c_str(), m_strClientName.c_str(), m_strEnv.c_str());
}
_Module.WriteDebugString(szBuffer);
}
catch(_com_error & e)
{
ATLTRACE("CContextATL::get_ContextString exception : %s\n", e.ErrorMessage());
}
return S_OK;
}
Anyone who could help me out to convert the following function from VB.NET to c#?
The VB property is a "parameterized property" - this is not available in C#, so you would convert this to 2 separate methods:
public string get_CtxString(string strItemType)
{
try
{
return myContext.ContextString(strItemType);
}
catch (Exception ex)
{
return "";
}
}
public void set_CtxString(string strItemType, string value)
{
try
{
myContext.ContextString(strItemType) = value;
}
catch (Exception ex)
{
}
}
Your original 'set' method is now redundant:
public void SetCtxString(string strItemType, string value)
{
try
{
myContext.ContextString(strItemType) = value;
}
catch (Exception ex)
{
}
}
How about this? C# can overload bracket operator.
I have no idea what's ContextString is, however, if ContextString is a Dictionary or the type uses brackets to get value, you can do it like this:
public string this[string strItemType]
{
get
{
try
{
return myContext.ContextString[strItemType];
}
catch (Exception ex)
{
return "";
}
}
set
{
try
{
myContext.ContextString[strItemType] = value;
}
catch (Exception ex) { }
}
}

C# run VBScript with MSScriptControl AddObject with string failed

This is my C# program:
class Program
{
static void Main(string[] args)
{
CallVbsFunction(1); //Work
CallVbsFunction(1.2); //Work
CallVbsFunction('a'); //Work
CallVbsFunction("a"); //!!Exception see bellow
}
private static void CallVbsFunction(object p)
{
var sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
sc.AllowUI = true;
try
{
sc.AddCode(System.IO.File.ReadAllText("script.vbs"));
sc.AddObject("myguid", p, false);
var parameters = new object[] { "a" };
sc.Run("test", ref parameters);
}
catch (Exception e)
{
Console.Out.WriteLine(e.ToString());
}
}
}
My VBScript file contents:
Function Test(a)
MsgBox myguid
End Function
And Finally this is my exception when I use AddObject() with string object:
System.Runtime.InteropServices.COMException (0x800A0005): Invalid
procedure call or argument at
MSScriptControl.IScriptControl.Run(String ProcedureName, Object[]&
Parameters) at Srcipting.Program.CallVbsFunction(Object p) in
Program.cs
You need to use a wrapper object that is ComVisible:
[ComVisible(true)]
public class StringWrapper
{
private string wrappedString;
public StringWrapper(string value)
{
wrappedString = value;
}
public override string ToString()
{
return wrappedString;
}
}
CallVbsFunction(new StringWrapper("a"));
The problem is that the .net String object looks like a native vb string to the MSScriptControl on the first look but not on the second look.
You only need to use this wrapper when you register a string directly or register a function that returns a string. There is no problem when registering an object that has properties of type string. There is also no problem for the parameters you pass to Run() because these will be correctly marshaled to native vb strings by the .net runtime.
So the maybe best option is to not provide individual strings to your script but an object that encapsulates all the different values you want it to use.
Define this class
[ComVisible(true)]
public class HostOptions
{
public string OptionA { get; set; }
public string OptionB { get; set; }
}
Then construct the object and set all the properties and register it with the script control
var hostOptions = new HostOptions();
hostOptions.OptionA = "AAA";
hostOptions.OptionB = "BBB";
sc.AddObject("HostOptions", hostOptions, false);
You can then use it in your script like this:
Function Test(a)
MsgBox HostOptions.OptionA
MsgBox HostOptions.OptionB
End Function

Give an Object a Method in C#

Is it possible to give a C# Object like
public string Name
{
get { return _name; }
set { _name = value; }
}
a Method doing something like:
private void addTextToName(){
_name = _name + " - Test";
}
so that I can call it like
Name.addTextToName();
Because (where I come from) in JavaScript you can do such things with .prototype
Is there any way to do this in C#?
If you are asking can I add a method to a string? then yes. Look at extension methods.
public static string AddTextToName(this string s)
{
return s + " - Test";
}
Use it like this:
"Hello".AddTextToName();
Will return Hello - test.
Yes, there is a way for C# Objects (you used a string there, but though...).
Take a look at the so-called "extension methods" in C# as they are exactly what you need I think.
For further reference, look e.g. here: https://msdn.microsoft.com/en-us/library/vstudio/bb383977%28v=vs.110%29.aspx (the magic is in the this as parameter for the method)
Using the extension method.
class Program
{
static void Main()
{
Example e = new Example();
e.Name = "Hello World";
var x = e.Name;
var y = x.addTextToName();
Console.WriteLine(y);
Console.ReadLine();
}
}
class Example
{
public string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public static class MyExtensions
{
public static string addTextToName(this string str)
{
return str += " - Test";
}
}

How to grab screenshot name

I have class that takes screenshot of the page and saves image date and time format that way I have unique screenshot. But I can not figure it out how can call this method outside of class to take screenshot and grab the name to print on my console. This is my Utility class I perform screenshot:
public class Utility
{
public static void TakeScreenshot()
{
String now = DateTime.Now.ToString("MM-dd-yyy hh-mm tt ");
try
{
Screenshot ss = ((ITakesScreenshot)Driver.Instance).GetScreenshot();
ss.SaveAsFile(#".\Screenshots\"+now+"Screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
}
Now I can call my screenshot class here in this method but how can I get new created screenshot name?
[Test]
public void ScreenshotTest()
{
Utility.TakeScreenshot(); //Here I can perform screenshot but how can I grab screenshot name
Console.Write(""); // So I can print here
}
You can change your method instead to return void, to return string and then return the name of your just taken screenshot
string ssName= now+"Screenshot.png";
Screenshot ss = ((ITakesScreenshot)Driver.Instance).GetScreenshot();
ss.SaveAsFile(#".\Screenshots\"+now+"Screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
at the end of your method use the return ssName;
and that will allow you to get the name as a return from your method
String ssName= Utility.TakeScreenshot();
Console.Write("ssName");
This is same as Csharls's answer. Just explained..
Utility Class
public class Utility
{
public static string TakeScreenshot()
{
String now = DateTime.Now.ToString("MM-dd-yyy hh-mm tt ");
string FileName = now + "Screenshot.png";
try
{
Screenshot ss = ((ITakesScreenshot)Driver.Instance).GetScreenshot();
ss.SaveAsFile(#".\Screenshots\" + FileName, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
return FileName;
}
}
Call Location
[Test]
public void ScreenshotTest()
{
string FileName;
FileName = Utility.TakeScreenshot(); //Here I can perform screenshot but how can I grab screenshot name
Console.Write(FileName); // So I can print here
}

Categories

Resources