Windows Form App With Inline Assembly Code - c++-cli

I am very confused and frustrated, I am in an assembly programming class and am tasked with modifying an example from the textbook for one on my assignment problems. The example is a simple string encrypter which uses inline assembly to modify characters from an input string one-by-one and print it back out in another textbox within a windows form.
I wrote everything exactly as it is in the text and no dice Visual Studio will not build it and tons of build errors are thrown. I don't understand what I am doing wrong, and it is frustrating when I cannot practically apply something that is demonstrated in the text.
Here is the example code:
Windows Form Data Encryption
And here is what I have which is in the header file .h that is part of the form design:
#pragma once
char EncryptionKey = 0x45;
char Encrypt(char code)
{
_asm
{
mov a1, code
xor a1, EncryptionKey
mov code, a1
mov a1, EncryptionKey
inc a1
and a1, 7fh
mov EncryptionKey, a1
}
return code;
}
namespace TextEncryter {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ Encrypt;
protected:
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::RichTextBox^ richTextBox2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->Encrypt = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->richTextBox2 = (gcnew System::Windows::Forms::RichTextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// Encrypt
//
this->Encrypt->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->Encrypt->Location = System::Drawing::Point(197, 249);
this->Encrypt->Name = L"Encrypt";
this->Encrypt->Size = System::Drawing::Size(75, 23);
this->Encrypt->TabIndex = 0;
this->Encrypt->Text = L"Encrypt";
this->Encrypt->UseVisualStyleBackColor = true;
this->Encrypt->Click += gcnew System::EventHandler(this, &Form1::Encrypt_Click);
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(13, 22);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(259, 96);
this->richTextBox1->TabIndex = 1;
this->richTextBox1->Text = L"";
//
// richTextBox2
//
this->richTextBox2->Location = System::Drawing::Point(13, 138);
this->richTextBox2->Name = L"richTextBox2";
this->richTextBox2->Size = System::Drawing::Size(259, 96);
this->richTextBox2->TabIndex = 2;
this->richTextBox2->Text = L"";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(13, 3);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(36, 16);
this->label1->TabIndex = 3;
this->label1->Text = L"Input";
this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label2->Location = System::Drawing::Point(10, 121);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(98, 16);
this->label2->TabIndex = 4;
this->label2->Text = L"Encrypted Text";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 284);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->richTextBox2);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->Encrypt);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
*private: System::Void Encrypt_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = "";
for (int a = 0; a < textBox1->Text->Length; a++)
{
richTextBox1->Text += Convert::ToChar(Encrypt(textBox1->Text[a]));
}
}*
};
}
I am using Visual Studio 2008 Express. When I try and use a newer version of visual studio windows forms are based on C# and not C++ and I can't even get it to build as far as in 2008. The main errors that are thrown are:
1> Inline native assembly not supported in managed code
error C3862: 'Encrypt': cannot compile an unmanaged function with /clr:pure or /clr:safe
C2227: left of '->Text' must point to class/struct/union/generic type
1> type is ''unknown-type''
Do I maybe need to put the char Encrypt function within the .ccp source file that belongs to the project? Any help would be greatly appreciated.

Related

Outlook VSTO AddIn / Custom Ribbon: How to add custom Ribbon to home group (Main Window) in Outlook 2016

I tried to add the following custom ribbon (Designer Approach) to the TabHomeGroup in Outlook (Explorer). Unfortunateyl it won't show up. Why?
I used these OfficeIds: https://www.microsoft.com/en-us/download/details.aspx?id=50745
namespace OutlookAddIn4
{
partial class Ribbon_Explorer : Microsoft.Office.Tools.Ribbon.RibbonBase
{
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
public Ribbon_Explorer()
: base(Globals.Factory.GetRibbonFactory())
{
InitializeComponent();
}
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">"true", wenn verwaltete Ressourcen gelöscht werden sollen, andernfalls "false".</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Komponenten-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.tab1 = this.Factory.CreateRibbonTab();
this.group1 = this.Factory.CreateRibbonGroup();
this.btnSettings = this.Factory.CreateRibbonButton();
this.tab1.SuspendLayout();
this.group1.SuspendLayout();
this.SuspendLayout();
//
// tab1
//
this.tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;
this.tab1.ControlId.OfficeId = "TabHomeGroup";
this.tab1.Groups.Add(this.group1);
this.tab1.Label = "TabHomeGroup";
this.tab1.Name = "tab1";
//
// group1
//
this.group1.Items.Add(this.btnSettings);
this.group1.Label = "group1";
this.group1.Name = "group1";
//
// btnSettings
//
this.btnSettings.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
this.btnSettings.Image = global::OutlookAddIn4.Properties.Resources.do_48x48;
this.btnSettings.Label = "Settings";
this.btnSettings.Name = "btnSettings";
this.btnSettings.ShowImage = true;
this.btnSettings.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.btnSettings_Click);
//
// Ribbon_Explorer
//
this.Name = "Ribbon_Explorer";
this.RibbonType = "Microsoft.Outlook.Explorer";
this.Tabs.Add(this.tab1);
this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon_Explorer_Load);
this.tab1.ResumeLayout(false);
this.tab1.PerformLayout();
this.group1.ResumeLayout(false);
this.group1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
internal Microsoft.Office.Tools.Ribbon.RibbonTab tab1;
internal Microsoft.Office.Tools.Ribbon.RibbonGroup group1;
internal Microsoft.Office.Tools.Ribbon.RibbonButton btnSettings;
internal SettingsForm settingsForm;
}
partial class ThisRibbonCollection
{
internal Ribbon_Explorer Ribbon_Explorer
{
get { return this.GetRibbon<Ribbon_Explorer>(); }
}
}
}
Are you sure the TabHomeGroup is visible at the moment your add-in is started?
Anyway, you need to use the TabHome idMso instead if you want to place your custom UI on the Home tab in the Explorer window.

Similar method to messageBox in .NET core

Is there any method to display pop up message like MessageBox.Show in .NET core?
One of the problems of a cheap replacement is, there's no equivalent function on Linux (or Mac).
So you can either write your own with many pinvokes and structures, or you can just use the "notify-send" command, which opens a "notification bubble" on Linux.
WindowManager.MsgBoxResult result = WindowManager.Interaction
.MsgBox("Message", "Title", MsgBoxStyle.OkCancel);
Here's my implementation:
namespace WindowManager
{
public enum MsgBoxResult
: int
{
Abort = 3,
Cancel = 2,
Ignore = 5,
No = 7,
Ok = 1,
Retry = 4,
Yes = 6
}
//[System.Flags]
//public enum MsgBoxStyle
// : int
//{
// AbortRetryIgnore = 2,
// ApplicationModal = 0,
// Critical = 0x10,
// DefaultButton1 = 0,
// DefaultButton2 = 0x100,
// DefaultButton3 = 0x200,
// Exclamation = 0x30,
// Information = 0x40,
// MsgBoxHelp = 0x4000,
// MsgBoxRight = 0x80000,
// MsgBoxRtlReading = 0x100000,
// MsgBoxSetForeground = 0x10000,
// OkCancel = 1,
// OkOnly = 0,
// Question = 0x20,
// RetryCancel = 5,
// SystemModal = 0x1000,
// YesNo = 4,
// YesNoCancel = 3
//}
[System.Flags]
public enum MsgBoxStyle
{
/// <summary>
/// OK button only (default). This member is equivalent to the Visual Basic constant <see langword="vbOKOnly" />.</summary>
OkOnly = 0,
/// <summary>
/// OK and Cancel buttons. This member is equivalent to the Visual Basic constant <see langword="vbOKCancel" />.</summary>
OkCancel = 1,
/// <summary>
/// Abort, Retry, and Ignore buttons. This member is equivalent to the Visual Basic constant <see langword="vbAbortRetryIgnore" />.</summary>
AbortRetryIgnore = 2,
/// <summary>
/// Yes, No, and Cancel buttons. This member is equivalent to the Visual Basic constant <see langword="vbYesNoCancel" />.</summary>
YesNoCancel = AbortRetryIgnore | OkCancel, // 0x00000003
/// <summary>
/// Yes and No buttons. This member is equivalent to the Visual Basic constant <see langword="vbYesNo" />.</summary>
YesNo = 4,
/// <summary>
/// Retry and Cancel buttons. This member is equivalent to the Visual Basic constant <see langword="vbRetryCancel" />.</summary>
RetryCancel = YesNo | OkCancel, // 0x00000005
/// <summary>Critical message. This member is equivalent to the Visual Basic constant <see langword="vbCritical" />.</summary>
Critical = 16, // 0x00000010
/// <summary>Warning query. This member is equivalent to the Visual Basic constant <see langword="vbQuestion" />.</summary>
Question = 32, // 0x00000020
/// <summary>Warning message. This member is equivalent to the Visual Basic constant <see langword="vbExclamation" />.</summary>
Exclamation = Question | Critical, // 0x00000030
/// <summary>Information message. This member is equivalent to the Visual Basic constant <see langword="vbInformation" />.</summary>
Information = 64, // 0x00000040
/// <summary>First button is default. This member is equivalent to the Visual Basic constant <see langword="vbDefaultButton1" />.</summary>
DefaultButton1 = 0,
/// <summary>Second button is default. This member is equivalent to the Visual Basic constant <see langword="vbDefaultButton2" />.</summary>
DefaultButton2 = 256, // 0x00000100
/// <summary>Third button is default. This member is equivalent to the Visual Basic constant <see langword="vbDefaultButton3" />.</summary>
DefaultButton3 = 512, // 0x00000200
/// <summary>Application modal message box. This member is equivalent to the Visual Basic constant <see langword="vbApplicationModal" />.</summary>
ApplicationModal = 0,
/// <summary>System modal message box. This member is equivalent to the Visual Basic constant <see langword="vbSystemModal" />.</summary>
SystemModal = 4096, // 0x00001000
/// <summary>Help text. This member is equivalent to the Visual Basic constant <see langword="vbMsgBoxHelp" />.</summary>
MsgBoxHelp = 16384, // 0x00004000
/// <summary>Right-aligned text. This member is equivalent to the Visual Basic constant <see langword="vbMsgBoxRight" />.</summary>
MsgBoxRight = 524288, // 0x00080000
/// <summary>Right-to-left reading text (Hebrew and Arabic systems). This member is equivalent to the Visual Basic constant <see langword="vbMsgBoxRtlReading" />.</summary>
MsgBoxRtlReading = 1048576, // 0x00100000
/// <summary>Foreground message box window. This member is equivalent to the Visual Basic constant <see langword="vbMsgBoxSetForeground" />.</summary>
MsgBoxSetForeground = 65536, // 0x00010000
}
// https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-messagebox
internal class UnsafeNativeMethods
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
internal static extern MsgBoxResult MessageBox(System.IntPtr hWnd, string text, string caption, MsgBoxStyle options);
}
public class Interaction
{
private static string GetTitleFromAssembly(System.Reflection.Assembly CallingAssembly)
{
try
{
return CallingAssembly.GetName().Name;
}
catch (System.Security.SecurityException)
{
string fullName = CallingAssembly.FullName;
int index = fullName.IndexOf(',');
if (index >= 0)
{
return fullName.Substring(0, index);
}
return "";
}
}
public static MsgBoxResult MsgBox(string text, string caption, MsgBoxStyle options)
{
if (string.IsNullOrEmpty(caption))
caption = GetTitleFromAssembly(System.Reflection.Assembly.GetCallingAssembly());
if (System.Environment.OSVersion.Platform != System.PlatformID.Unix)
return UnsafeNativeMethods.MessageBox(System.IntPtr.Zero, text, caption, options);
text = text.Replace("\"", #"\""");
caption = caption.Replace("\"", #"\""");
using (System.Diagnostics.Process p = System.Diagnostics.Process.Start("notify-send", "\"" + caption + "\" \"" + text + "\""))
{
p.WaitForExit();
}
return MsgBoxResult.Ok;
}
public static MsgBoxResult MsgBox(string text, string caption)
{
return MsgBox(text, caption, MsgBoxStyle.OkOnly);
}
public static MsgBoxResult MsgBox(string text)
{
return MsgBox(text, null);
}
public static MsgBoxResult MsgBox(object objText, object objCaption)
{
string text = System.Convert.ToString(objText, System.Globalization.CultureInfo.InvariantCulture);
string caption = System.Convert.ToString(objCaption, System.Globalization.CultureInfo.InvariantCulture);
return MsgBox(text, caption);
}
public static MsgBoxResult MsgBox(object objText)
{
return MsgBox(objText, null);
}
} // End Class Interaction
} // End Namespace WindowManager
Update:
Yes. .NetCore 3.0+ supports WPF/Windows Forms
As John Dyer noted in comments,
VB users can use this for a message box:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.messagebox?view=netcore-3.1
Original answer 2017:
No. .NET Core is focused on web, Messagebox is a winforms thing. And there is no winforms support in .NET Core.
A more-or-less official answer on "Why is there no Microsoft.UI.WinForms .Net Core package"
My team (the Microsoft .NET team) is focused on .NET Core as a cloud
and web framework. We know that folks would like us to build
additional application types for .NET Core. For now, we're remaining
focused. Many other developers want us to stay focused on what we've
started with .NET Core and that's our intention.
https://github.com/dotnet/core/issues/374

Can't compile thread example in Visual C++ 2005 using "oldSyntax" switch

I am trying to follow this example on how to create threads in a windows form. Purely following the example I get multiple syntax errors upon building.
I am using the /clr:oldSyntax compiler switch to compile the example.
Initialization of the Form1 class is the first source of the error:
public ref class Form1 : public System::Windows::Forms::Form
{
Errors:
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2143: syntax error : missing ';' before '{'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2447: '{' : missing function header (old-style formal list?)
Where exactly are these errors coming from?
Full Code:
#pragma once
using namespace System::Threading;
namespace ThreadExample
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::ProgressBar^ progressBar1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: Thread *trd;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(197, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(94, 162);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 23);
this->progressBar1->TabIndex = 1;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
ThreadStart *myThreadDelegate = new ThreadStart(this, repeat);
trd = new Thread(myThreadDelegate);
trd->IsBackground = true;
trd->Start();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show(S"This is the main thread");
}
private: System::Void progressBar1_Click(System::Object^ sender, System::EventArgs^ e)
{
}
};
__delegate void DelegateThreadTask();
private: void ThreadTask()
{
int stp;
int newval;
Random *rnd=new Random();
if (progressBar1->InvokeRequired == false)
{
stp=this->progressBar1->Step*rnd->Next(-1,2);
newval = this->progressBar1->Value + stp;
if (newval > this->progressBar1->Maximum)
newval = this->progressBar1->Maximum;
else if (newval < this->progressBar1->Minimum)
newval = this->progressBar1->Minimum;
this->progressBar1->Value = newval;
}
else
{
DelegateThreadTask *myThreadDelegate = new DelegateThreadTask(this,ThreadTask);
this->Invoke(myThreadDelegate);
}
}
private: void repeat()
{
while(true)
{
ThreadTask();
Thread::Sleep(100);
}
}
}
The old syntax for C++/CLI uses __gc to declare classes.
References:
C++/CLI language specification from Visual Studio 2003, when the 'old' syntax was the only syntax.
VS2005 documentation giving both old & new syntax for declaring managed types in C++/CLI.
Test 1:
public ref class Foo
{
};
Compile result:
OldSyntax.cpp
OldSyntax.cpp(1): error C2059: syntax error : 'public'
OldSyntax.cpp(2): error C2143: syntax error : missing ';' before '{'
OldSyntax.cpp(2): error C2447: '{' : missing function header (old-style formal list?)
Test 2:
public __gc class Foo
{
};
Compile result: no errors.
(Note: I used VS2012 for my test. I don't have VS2005 installed anymore; the error messages may be slightly different on 2005.)

How to get access to the VSTO Application object of multiple instances of Visio 2010 using C#?

Does anyone know how to get access to the VSTO Application object of multiple instances of Visio 2010 using C#? Marshal.GetActiveObject() only returns the active instance.
There are forum posts on how to get multiple Application objects for all instances of Excel - where the author is using the technique to iterate through each Excel instance by process, get the native object model from a child window of the Excel instance, and finally to get the Application object.
This technique works well for for Excel, but I am unable to get a valid native OM (IDispatch or IAccessible) from the call to AccessibleObjectFromWindow() for a Visio window handle that would allow me to reference the parent Application object.
Here's my code snippet:
using VISIO = Microsoft.Office.Interop.Visio;
Process[] processes = Process.GetProcessesByName(Visio.APP_PROCESS_NAME);
foreach (Process process in processes)
{
int hwnd = (int)process.MainWindowHandle;
// We need to enumerate the child windows to find one that
// supports accessibility. To do this, instantiate the
// delegate and wrap the callback method in it, then call
// EnumChildWindows, passing the delegate as the 2nd arg.
if (hwnd != 0)
{
int hwndChild = 0;
cb = new EnumChildCallback(EnumChildProc);
EnumChildWindows(hwnd, cb, ref hwndChild);
// If we found an accessible child window, call
// AccessibleObjectFromWindow, passing the constant
// OBJID_NATIVEOM (defined in winuser.h) and
// IID_IDispatch - we want an IDispatch pointer
// into the native object model.
if (hwndChild != 0)
{
const uint OBJID_NATIVEOM = 0xFFFFFFF0;
Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
EXL.Window ptr = null;
int hr = AccessibleObjectFromWindow(
hwndChild, OBJID_NATIVEOM,
IID_IDispatch.ToByteArray(), ref ptr);
if (hr >= 0)
{
// If we successfully got a native OM
// IDispatch pointer, we can QI this for
// an Viso Application (using the implicit
// cast operator supplied in the PIA).
VISIO.Application app = (VISIO.Application)ptr.Application;
allInstances.Add(app);
}
}
}
}
I found a solution that iterates through the Runtime Object Table (ROT) to get all monikers corresponding to Visio documents (".vdx") and get their corresponding VSO.Document object (using VSO=Microsoft.Office.Interop.Visio); with the VSO.Document object, I can get the VSO.Application object from the Document.Application property.
From this article by Andrew Baker: http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=69&PostID=1, I created a ROTUtil utility/helper class:
/// <summary>
/// The COM running object table utility class.
/// </summary>
public class ROTUtil
{
#region APIs
[DllImport("ole32.dll")]
private static extern int GetRunningObjectTable(int reserved,
out UCOMIRunningObjectTable prot);
[DllImport("ole32.dll")]
private static extern int CreateBindCtx(int reserved,
out UCOMIBindCtx ppbc);
[DllImport("ole32.dll", PreserveSig = false)]
private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("ole32.dll", PreserveSig = false)]
private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("ole32.dll")]
private static extern int ProgIDFromCLSID([In()]ref Guid clsid, [MarshalAs(UnmanagedType.LPWStr)]out string lplpszProgID);
#endregion
#region Public Methods
/// <summary>
/// Converts a COM class ID into a prog id.
/// </summary>
/// <param name="progID">The prog id to convert to a class id.</param>
/// <returns>Returns the matching class id or the prog id if it wasn't found.</returns>
public static string ConvertProgIdToClassId(string progID)
{
Guid testGuid;
try
{
CLSIDFromProgIDEx(progID, out testGuid);
}
catch
{
try
{
CLSIDFromProgID(progID, out testGuid);
}
catch
{
return progID;
}
}
return testGuid.ToString().ToUpper();
}
/// <summary>
/// Converts a COM class ID into a prog id.
/// </summary>
/// <param name="classID">The class id to convert to a prog id.</param>
/// <returns>Returns the matching class id or null if it wasn't found.</returns>
public static string ConvertClassIdToProgId(string classID)
{
Guid testGuid = new Guid(classID.Replace("!", ""));
string progId = null;
try
{
ProgIDFromCLSID(ref testGuid, out progId);
}
catch (Exception)
{
return null;
}
return progId;
}
/// <summary>
/// Get a snapshot of the running object table (ROT).
/// </summary>
/// <returns>A hashtable mapping the name of the object in the ROT to the corresponding object
/// <param name="filter">The filter to apply to the list (nullable).</param>
/// <returns>A hashtable of the matching entries in the ROT</returns>
public static Hashtable GetActiveObjectList(string filter)
{
Hashtable result = new Hashtable();
int numFetched;
UCOMIRunningObjectTable runningObjectTable;
UCOMIEnumMoniker monikerEnumerator;
UCOMIMoniker[] monikers = new UCOMIMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
monikerEnumerator.Reset();
while (monikerEnumerator.Next(1, monikers, out numFetched) == 0)
{
UCOMIBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
if (filter == null || filter.Length == 0 || runningObjectName.IndexOf(filter) != -1)
{
object runningObjectVal;
runningObjectTable.GetObject(monikers[0], out runningObjectVal);
result[runningObjectName] = runningObjectVal;
}
}
return result;
}
/// <summary>
/// Returns an object from the ROT, given a prog Id.
/// </summary>
/// <param name="progId">The prog id of the object to return.</param>
/// <returns>The requested object, or null if the object is not found.</returns>
public static object GetActiveObject(string progId)
{
// Convert the prog id into a class id
string classId = ConvertProgIdToClassId(progId);
UCOMIRunningObjectTable prot = null;
UCOMIEnumMoniker pMonkEnum = null;
try
{
int Fetched = 0;
// Open the running objects table.
GetRunningObjectTable(0, out prot);
prot.EnumRunning(out pMonkEnum);
pMonkEnum.Reset();
UCOMIMoniker[] pmon = new UCOMIMoniker[1];
// Iterate through the results
while (pMonkEnum.Next(1, pmon, out Fetched) == 0)
{
UCOMIBindCtx pCtx;
CreateBindCtx(0, out pCtx);
string displayName;
pmon[0].GetDisplayName(pCtx, null, out displayName);
Marshal.ReleaseComObject(pCtx);
if (displayName.IndexOf(classId) != -1)
{
// Return the matching object
object objReturnObject;
prot.GetObject(pmon[0], out objReturnObject);
return objReturnObject;
}
}
return null;
}
finally
{
// Free resources
if (prot != null)
Marshal.ReleaseComObject(prot);
if (pMonkEnum != null)
Marshal.ReleaseComObject(pMonkEnum);
}
}
Then here's the method in my class to get the instances:
/// <summary>
/// This strategy of getting the VSO.Application instances uses the appropriate file monikers
/// from the Runtime Object Table to get the VSO.Document object and hence the VSO.Document.Application
/// property.
/// </summary>
/// <param name="allInstances"></param>
protected void GetInstancesROTStrategy(IList<VSO.Application> allInstances)
{
// Iterate through all the objects in the ROT
string filter = ".vdx";
Hashtable runningObjects = ROTUtil.GetActiveObjectList(filter);
Hashtable appInstances = new Hashtable();
// Display the object ids
foreach (DictionaryEntry de in runningObjects)
{
// get visio document file monikers
string progId = de.Key.ToString();
object getObj = ROTUtil.GetActiveObject(progId);
if (getObj != null)
{
// try to cast the object to a VSO.Document
VSO.Document doc = getObj as VSO.Document;
if (doc != null)
{
VSO.Application app = doc.Application;
// only add to results IList if not duplicate
if (!appInstances.ContainsKey(app.WindowHandle32))
{
appInstances.Add(app.WindowHandle32, app);
allInstances.Add(app);
}
}
}
}
}

Exception in Entity Framework: Error 0194: All artifacts loaded

This is a c#/asp.net project. The full error message I get is:Error 0194: All artifacts loaded into the item collection must have the same version. Multiple versions were encountered.
This project was started as a 3.5 and upgraded to 4.0. When I try to test any of the methods I get the error that I posted in the subject line. I am going to include the actual lines that it throws the exception on. If there is anything in people need to see to try to help let me know and I post it as well. Any help will be appreciated, I am having no luck with this.
/// <summary>
/// Initializes a new SFBExternalPaymentsEntities object using the connection string found in the 'SFBExternalPaymentsEntities' section of the application configuration file.
/// </summary>
public SFBExternalPaymentsEntities() : base("name=SFBExternalPaymentsEntities", "SFBExternalPaymentsEntities")
{
this.ContextOptions.LazyLoadingEnabled = false;
OnContextCreated();
}
/// <summary>
/// Initialize a new SFBExternalPaymentsEntities object.
/// </summary>
public SFBExternalPaymentsEntities(string connectionString) : base(connectionString, "SFBExternalPaymentsEntities")
{
this.ContextOptions.LazyLoadingEnabled = false;
OnContextCreated();
}
/// <summary>
/// Initialize a new SFBExternalPaymentsEntities object.
/// </summary>
public SFBExternalPaymentsEntities(EntityConnection connection) : base(connection, "SFBExternalPaymentsEntities")
{
this.ContextOptions.LazyLoadingEnabled = false;
OnContextCreated();
}
#endregion
Added a method calling the constructor.
public static CreditCardResponse AuthCapture(CreditCard newCC)
{
ACHResponse validateResponse = CreditCard.Validate(newCC);
if (validateResponse.Status == "Accepted")
{
Profile currentProfile = new Profile();
currentProfile = ProfilesGateWay.GetByID(newCC.ProfileID);
CreditCardTransaction newCCTransaction = CreateCreditCardTransaction(newCC, currentProfile);
ServiceClient client = new ServiceClient();
CreditCardTransactionResponse cctResponse = client.CreditCardAuthorizeAndCapture(newCCTransaction);
client.Close();
CreditCardResponse ccResponse = CreateCCResponse(cctResponse);
if (ccResponse.ResponseCode == 1)
{
int authAVS = ConvertAVStoInt(ccResponse.AVSResponse);
int appAVS = ConvertAVStoInt(newCC.AVLevel);
bool isAVSPass = CompareAVS(authAVS, appAVS);
if (isAVSPass == false)
{
ccResponse.ResponseCode = 0;
ccResponse.ResponseReasonCode = 99;
ccResponse.ResponseText = "Did not meet your AVS requirements";
return ccResponse;
}
else
{
int newCCID = CreateCreditCardRecord(newCC, currentProfile, cctResponse, "Captured", "Auth_Capture");
CreditCardRecord updateCC = CreditCardRecordsGateWay.GetByID(newCCID);
updateCC.CaptureOn = DateTime.Now;
CreditCardRecordsGateWay.Update(updateCC);
return ccResponse;
}
}
else
{
return ccResponse;
}
}
CreditCardResponse newCCResponse = new CreditCardResponse();
newCCResponse.ResponseCode = 0;
newCCResponse.AchResponse = validateResponse;
return newCCResponse;
}
public static CreditCardResponse PriorAuthCapture(CreditCard newCC)
{
CreditCardRecord ccRecord = CreditCardRecordsGateWay.GetByCCGateWayID(newCC.CreditCardTransactionID);
ServiceClient client = new ServiceClient();
CreditCardTransaction ccTransaction = client.CreditCardGetTransactionById(ccRecord.CCGatewayID);
CreditCardTransactionResponse cctResponse = client.CreditCardPriorAuthorizationCapture(ccTransaction);
if (cctResponse.ResponseCode == 1)
{
ccRecord.Status = "Captured";
ccRecord.CaptureOn = DateTime.Now;
}
CreditCardResponse ccResponse = CreateCCResponse(cctResponse);
return ccResponse;
}
protected static int CreateCreditCardRecord(CreditCard newCC, Profile currentProfile, CreditCardTransactionResponse cctResponse, string status, string transactionType)
{
CreditCardRecord newCCRecord = new CreditCardRecord();
newCCRecord.Address = newCC.Address;
newCCRecord.AddressVerificationLevel = newCC.AVLevel;
newCCRecord.Amount = newCC.Amount;
newCCRecord.CardCode = newCC.CardCode;
newCCRecord.CardNumber = newCC.CardNumber;
newCCRecord.CCGatewayID = cctResponse.CreditCardTransactionID;
newCCRecord.City = newCC.City;
newCCRecord.CompanyName = newCC.CompanyName;
newCCRecord.CreateBy = currentProfile.ACHCompanyName;
newCCRecord.CreateOn = DateTime.Now;
newCCRecord.Description = newCC.Description;
newCCRecord.Expiration = newCC.Expiration;
newCCRecord.FirstName = newCC.FirstName;
newCCRecord.LastName = newCC.LastName;
newCCRecord.Profile.ProfileID = currentProfile.ProfileID;
newCCRecord.State = newCC.State;
newCCRecord.Status = status;
newCCRecord.TransactionType = transactionType;
newCCRecord.Zip = newCC.Zip;
return CreditCardRecordsGateWay.Insert(newCCRecord);
}
You have just posted the constructors to your SFBExternalPaymentsEntities class, but you seem to say that you get an exception when you call a method that returns a collection of entities, which makes send with the exception message you are getting. The chances are the problem lies within the method you are calling, or in the code calling it rather than in the constructor code you have posted. Can you post some more relevent code or explain how the code you have posted relates to the exception.
I encountered the same problem when i updated one of my projects from .net 4.0 to 4.5, the reason probably was because there was another .edmx file in an other project that was referenced in the target project.
Solution to issue : Updated the other project that was referenced and contained .edmx file to .net 4.5 and then, Right-Clicked on every .edmx files in these 2 projects and then selected 'Run Custom Tool'
It worked for me, hope that it works for everyone reading this article