is there any function to have the same possibilities like "mono_add_internal_call" in the CLR-Hosting w/o using mono?
Mono
C++ Code:
static MonoString* Sample ()
{
return mono_string_new (mono_domain_get(), "Hello!");
}
mono_add_internal_call ("Hello::Sample", Sample);
C# Code:
using System;
using System.Runtime.CompilerServices;
class Hello {
[MethodImplAttribute(MethodImplOptions.InternalCall)]
extern static string Sample ();
}
Thanks
Instead of "mono_add_internal_call", I use now P/Invoke. It has the same results. So if you call an C# DLL with CLR-Hosting, the P/Invoke calls the CLR-Hosting Dll and not create a new instance.
Thanks to Lucas.
Related
I've been trying to convert this WinRT interface from its C++ Origin Code, to C# based code:
IDL File:
namespace Windows.Internal.Security.Authentication.Web{
[version(1)]
[uuid(07650a66-66ea-489d-aa90-0dabc75f3567)]
interface ITokenBrokerInternalStatics : IInspectable {
HRESULT filler_GetTokenSilently();
HRESULT filler_GetSecureInputParameters();
HRESULT filler_ReportBackgroundCompletion();
HRESULT filler_FindAccount();
HRESULT filler_FindAccountForApp();
HRESULT filler_FindAccountForProvider();
HRESULT FindAllAccountsAsync(
[out][retval] Windows.Foundation.IAsyncOperation<Windows.Foundation.Collections.IVectorView<Windows.Security.Credentials.WebAccount > *> ** operation);
}
[version(1)]
[static(Windows.Internal.Security.Authentication.Web.ITokenBrokerInternalStatics, 1)]
[marshaling_behavior(agile)]
[threading(both)]
runtimeclass TokenBrokerInternal {
}
}
CPP File:
auto tokenBrokerStatics = get_activation_factory<TokenBrokerInternal, Windows::Foundation::IUnknown>();
auto statics = tokenBrokerStatics.as<ITokenBrokerInternalStatics>();
auto accounts = statics.FindAllAccountsAsync().get();
This is what I got so far:
using PInvoke;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Internal.Security.Authentication.Web;
using Windows.Security.Credentials;
using GuidAttribute = System.Runtime.InteropServices.GuidAttribute;
namespace Windows.Internal.Security.Authentication.Web
{
[Version(1)]
[Guid("07650a66-66ea-489d-aa90-0dabc75f3567")]
[InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
public interface ITokenBrokerInternalStatics
{
Windows.Foundation.IAsyncOperation<IReadOnlyList<Windows.Security.Credentials.WebAccount>> FindAllAccountsAsync();
}
[Version(1)]
[MarshalingBehavior(MarshalingType.Agile)]
[Threading(ThreadingModel.Both)]
[Static(typeof(Windows.Internal.Security.Authentication.Web.ITokenBrokerInternalStatics), 1)]
public class TokenBrokerInternal { }
public static class TokenBrokerInternalAssist
{
public static ITokenBrokerInternalStatics GetTokenBrokerInternal()
{
var tokenBrokerStatics = WindowsRuntimeMarshal.GetActivationFactory(typeof(TokenBrokerInternal));
var statics = (ITokenBrokerInternalStatics)tokenBrokerStatics.ActivateInstance()
return statics;
}
}
}
However, I can't seem to get it to work despite my best efforts. Here's a couple of errors I've encountered while trying to get this working:
Casting Errors
Activation Factories Not Existing
Many Other Errors related to dumb mistakes
I'm pretty new at this WinRT stuff, but despite looking on multiple sites for answers to my problem, I've found myself at a dead-end, and I really just wanna convert this code to C# to avoid having to compile a C++ project twice to support Any CPU and the complicated compiling of my app. What am I doing wrong?
P.S. This is not a UWP app, so don't suggest WebAccountManager or anything UWP specific, please.
I looked at other posts and couldn't find the solution.
I am trying to use C# dll I created in VBA code without having to add reference.
In my VBA code, I declared:
Public Declare Function message Lib "path_to_my_dll" _
(ByVal message As String) As String
Sub Test()
Dim hello As String
hello = message("hi!!")
Debug.Print hello
End Sub
I get an error saying entry point for my dll couldn't be found.
The C# Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace DLLImport
{
public class Class1
{
[DllImport("DLLImport", EntryPoint = "Run")]
extern string Run(string message)
{
return message;
}
}
}
Thank you in advance for your help!!
You might want to use InteropServices to make a COM Visible DLL
using System.Runtime.InteropServices;
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("your-GUID-1")]
public interface _Visible_Methods
{
//--------< _Visible_Methods >--------
//*visible COM Methods of this Control under Office,Excel, Word
string get_Hello();
//--------</ _Visible_Methods >--------
}
Source: https://codedocu.com/Net-Framework/Controls/COM-ActiveX/Create-C_hash_-COM-Control-for-Office?2382
I have written a code in c++ and c# . From my c++ code i am calling my c# function through. I have sent just a part of c++ code.
txtPath contains the location of a text file.
C++ code:
CoInitialize(NULL);
IMyClassPtr obj3;
obj3.CreateInstance(__uuidof(Program));
obj3->Validation(txtPath);
CoUninitialize();
Validation() is my c# function.
My c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ABC
{
[ComVisible(true)]
public interface IMyClass
{
void Validation(string txtp);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Program : IMyClass
{
private string replace_string(string text)
{
return text.Replace("\r\a", "");
}
public void Validation(string txtp)
{
string[] textValidate = File.ReadAllLines(txtp);
string textpath = txtp;
//validation starts here
foreach (string line in textValidate)
{
string[] strsplit = line.Split(new string[] { "," }, StringSplitOptions.None);
string task = strsplit[0];
string sign = strsplit[1];
string person = strsplit[2];
string routing = strsplit[3];
if (String.IsNullOrEmpty(task) || String.IsNullOrEmpty(sign) || String.IsNullOrEmpty(person))
{
//if the txt file is invalid
MessageBox.Show("Signature.txt is incomplete or has invalid input!!!");
}
}
}
}
}
I have done all the required setting in c# .C# settings snapshot C# project is a class library. My code was working perfectly in 32 bit machine. I was using the generated tlb in other systems by registering it with regasm.exe.
In 64 bit machine by c++ code is working but when the c# linking code is hit the execution stops without throwing any error. I am using a 64 bit machine and created a new project with the same code. Help Please
Make you're using the correct version of 'regasm.exe' for your target platform (i.e. "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe"). Take note of 'Framework64'.
On 64-bit Windows, Microsoft does not support loading a 64-bit DLL into a 32-bit process, or vice-versa. For additional information, please refer to the following resource on MSDN:
http://msdn.microsoft.com/en-us/library/aa384231(VS.85).aspx
I want to enter the following command into the debugger
po [[UIWindow keyWindow] _autolayoutTrace]
How would I do that in Xamarin Studio?
Edit:
I saw this Objective-C code
NSLog(#"%#", [[UIWindow keyWindow] _autolayoutTrace]);
in programatically change autolayout on orientation change together with an interface definition for that.
// for debugging auto layout.
#interface UIWindow (AutoLayoutDebug)
+(UIWindow *)keyWindow;
-(NSString *)_autolayoutTrace;
#end
I'd like to know how this can be done in Xamarin iOS?
If you are using the unified API the solution posted by miguel.de.icaza doesn't work anymore, because you get
`ObjCRuntime.Messaging' is inaccessible due to its protection level
One has to use P/Invoke:
using System;
using System.Runtime.InteropServices;
using Foundation;
using UIKit;
using ObjCRuntime;
public static class UIViewAutolayoutTraceExtensions
{
[DllImport(Constants.ObjectiveCLibrary, EntryPoint="objc_msgSend")]
private static extern IntPtr IntPtr_objc_msgSend (IntPtr receiver, IntPtr selector);
public static NSString AutoLayoutTrace(){
return (NSString)Runtime.GetNSObject(IntPtr_objc_msgSend(UIApplication.SharedApplication.KeyWindow.Handle, new Selector ("_autolayoutTrace").Handle));
}
public static NSString RecursiveDescription(){
return (NSString)Runtime.GetNSObject(IntPtr_objc_msgSend(UIApplication.SharedApplication.KeyWindow.Handle, new Selector ("recursiveDescription").Handle));
}
}
Then you can use it like this:
Console.WriteLine(UIViewAutolayoutTraceExtensions.RecursiveDescription ());
One caveat though: You can only call this code after everything has been loaded. Otherwise KeyWindow will be null. So there is no possibility to set a breakpoint and then call this code. You can only call this code if you provide a button or something similar with which you trigger the action.
using MonoTouch.ObjCRuntime;
var str = new NSString (Messaging.IntPtr_objc_msgSend (UIApplication.SharedApplication.KeyWindow.Handle, new Selector ("_autolayoutTrace").Handle));
And "str" will contain the string that you can then use with Console.WriteLine
after along time of searching via google, I decided to poste my problem here.
First: I am total C# Noob. I am using a Macro Recorder from Jitbit and I have no choice to use a different. The Problem is in the Macro Recorder, it is missing some essential things.
Like reading a text file into a variable and paste this variable via Clipboard :-(
However the good thing is, the tool support "some" type of native C# Code
If I open the C# Command I get this:
public class Program
{
public static void Main()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
And the C# program has to follow also these rules:
=> This Code MUST contain a class named "Program" with a static method "Main"
I already used google and found code that should do the job but I get errors, I guess the
code doesn`t follow the above rules.
This is what I found and tried:
using System;
using System.IO;
public class Program
{
public static void Main()
{
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("Counter.txt");
string counter = myFile.ReadToEnd();
myFile.Close();
// Load string into clipboard
Clipboard.SetDataObject( counter, true );
}
}
I always get the error : "Line 15: The Name Clipboard is not existing in the context"?!?
I hope that someone can explain a noob (me) what is wrong and what is the correct code.
Thanks.
add reference to System.Windows.Forms
using System;
using System.IO;
using System.Windows.Forms;
public class Program
{
[STAThread]
public static void Main()
{
Clipboard.SetDataObject(File.ReadAllText("Counter.txt"), true);
}
}
Note that to Avoid the ThreadStateException you need to applying the STAThread attribute to your Main() function