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
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 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
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.
I am using visual studio 2010 AddIn template for ArcGis 10.0. Since I already wrote a python code for ArcGIS processing. I would like to insert a button into the ArcMap desktop command bar running this python code. Following is my simplized c# code and the beginning of python code for your reference:
c# code:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.VisualBasic;
namespace CallPython
{
public class CallPython : ESRI.ArcGIS.Desktop.AddIns.Button
{
public CallPython()
{
}
public string inputName()
{
string inputNames;
inputNames = "cavert.shp";
return inputNames;
}
protected override void OnClick()
{
inputName();
System.Diagnostics.Process.Start(
"D:\\From2\\BCAP\\python\\scripts\\trans5.py");
}
protected override void OnUpdate()
{
Enabled = ArcMap.Application != null;
}
}
}
Python code:
import arcpy
import os, win32com.client
from arcpy import env
arcpy.env.overwriteOutput = True
# Set environment settings
env.workspace = "D:/BCAP/trans/tempt/"
# Set local variables
vs_method_source = os.path.abspath("D:/Users/mar/CallPython/CallPython.cs")
vs_file_name = os.path.abspath("D:/Users/mar/CallPython/CallPython.sln")
vs = win32com.client.Dispatch("VisualStudio.DTE.7.1")
doc_template = vs.Documents.Open(vs_file_name)
mm = doc_template.CallPython.inputName() #CallPython is namespace, inputName is method
The last line of python code is the problem. I know it is definitely not right, but didn't know how to access C# namespace, class, then method from python.
Ok so I have an assembly, written in C#, using Visual Studio 2010.
This Assembly contains one class, which contains one method which returns the word Result, the code is below:
using System.Runtime.InteropServices;
namespace TestDLL
{
public class Class1
{
[ComVisible(true)]
public string TestMethod()
{
return "Result";
}
}
}
The output section in the Build tab on the properties window looks like so:
When I click on Build, I get a DLL file and a TLB file. I can add this TLB file to Microsoft Access simply by browsing to it.
Now, in Access I have a button and a label. I want to make the Caption property of my label equal to the result of testMethod. I'm thinking I need to do something similar to below but I'm not sure, any help would be much appreciated:
Private Sub btnMain_Click()
Dim tm As TestDLL
Dim foo As String
foo = tm.testMethod
lblBarr.Caption = foo
End Sub
Thankyou
Maybe next will work:
Private Sub btnMain_Click()
Dim tm As TestDLL.Class1
Dim foo As String
Set tm = New TestDLL.Class1
foo = tm.testMethod
lblBarr.Caption = foo
End Sub