I've been trying to run the sample project provided with the tutorial at stockbotprogramming http://www.stockbotprogramming.com/sharpcibtutorial1.php , but I keep getting a COMException every time I run the application.
I have the TWS Client running and the sample VB projects provided with the API are able to connect just fine, but when I try to use the C# sample provided by the tutorial then I get the following exception:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in System.Windows.Forms.dll
Th exception happens when I try to add the TWS ActiveX control:
namespace CSharpTutorial1
{
public partial class Form1 : Form
{
private AxTWSLib.AxTws tws;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tws = new AxTWSLib.AxTws();
tws.BeginInit();
tws.Enabled = true;
tws.Location = new System.Drawing.Point(32, 664);
tws.Name = "tws";
Controls.Add(tws); // <-- EXCEPTION HERE!
tws.EndInit();
tws.connect("127.0.0.1", 7496, 0);
String msg = "Connected to TWS server version " + tws.serverVersion + "at " + tws.TwsConnectionTime;
MessageBox.Show(msg);
}
}
}
The original project was probably done with Visual Studio 2005, but I have Visual Studio 2008 and it automatically converted the project (I've been reading that there are some problems there). Does anybody know what could be causing this exception? Any ideas on how to fix it?
Important: Make sure you add the component to the IDE using the visual designer. First right click in the toolbox, click "choose items", click the COM tab, then check the TWS control. Now using the visual IDE drag this item to the surface of your form. It must be visible in your app. (Don't create it in the Form1_Load())
Do something like this... (don't use an IP Address of 127.0.0.1, leave it blank):
axTws1.connect("", 7496, 0);
axTws1.reqMktData(0, "AMD", "STK", "", 0, "", "", "SMART", "ISLAND", "USD", "", 0);
Last (if you're using 64-bit Windows) compile the project in 32-bit code. Go to the configuration manager and then create a new profile "x86" and set all the configurations to x86.
Related
Is there anyway that a reference can be added to a solution programmatically?
I have an add-in button, when the user presses it, I want a reference to be added.
The reason is, I have created a piece of software that I want to be integrated into any given VS program (if the developer wants it), they would simply click the add-in button and the reference would be loaded in the current solution.
Is this possible?
Something like this I haven't tested it
get the environment
EnvDTE80.DTE2 pEnv = null;
Type myType = Type.GetTypeFromProgID("VisualStudio.DTE.8.0");
pEnv = (EnvDTE80.DTE2)Activator.CreateInstance(myType, true);
get the solution.
Solution2 pSolution = (Solution2)pEnv.VS.Solution;
get the project that you want
Project pProject = pSolution.Projects[0];
add the reference
pProject.References.Add(string referenceFilePath);
There is an example on CodeProject.
The functionality is contained within a single class elRefManager and the method to call is CheckReferences. The code can be looked at here by selecting the elRefManager.cs file on the left hand side.
As seen in the article you could do...
private void button1_Click(object sender, System.EventArgs e)
{
int ec;
ec=elRefManager.CheckReferences(null, new string[] {textBox1.Text});
if (ec<0)
MessageBox.Show("An error occurred adding this reference");
if (ec>0)
MessageBox.Show("Could not add " + textBox1.Text +
"\nCheck its spelling and try again");
}
System.Assembly.load Allows you to call functions in a library that were not built with your program.
If you want to add a reference to the project so that its in the solution you can use the following. Basically the same as #Scots answer.
I did it in a macro which is vb but I'm sure you can get the idea
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
Dim objProject As EnvDTE.Project
Dim i As Long
i = DTE.Solution.Projects.Count
For Each objProject In DTE.Solution.Projects
If (objProject.Name() = "csCA") Then
Dim vsproj As VSLangProj.VSProject
vsproj = objProject.Object
vsproj.References.Add("C:\Users\test.dll")
End If
Next
I want to have a Windows Form Application use a menustrip with three options to launch a console application. The console application is a .exe file built in C# in Visual Studio with some basic code for as school project. The console application does not need to return any values, it only needs to run and allow the user to use it. This is what the form will look like: Menu Application
I have tried importing the System.Diagnostics.Process.Start namespace with Process.Start#("Path of file") in my menu item click event method to launch my C# console application but have not been successful. I am getting a "Win32Exception was unhandled: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll. Additional information: The system cannot find the file specified"
Here is the code in the menu item click event:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lesson13LabCToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(#"\C:\Users\Sam\Documents\Visual Studio 2015\Projects\LabMenu\LabMenu\Lesson13LabC.exe");
}
}
Any ideas on what I am doing wrong?
The error is clear The system cannot find the file specified. Check the path of file.
Also remove the starting \ in the path
Remove the backslash at the beginning of your path (before the drive letter).
I have developed a COM+ Component in C# be inheriting ServicedComponent.
Here is how it looks like:
[Transaction(TransactionOption.Required)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[EventTrackingEnabledAttribute(true)]
[JustInTimeActivation]
[ObjectPooling(Enabled = true, MinPoolSize = 10, MaxPoolSize = 30, CreationTimeout = 15000)]
[Synchronization]
class MyComponent: System.EnterpriseServices.ServicedComponent
{
[AutoComplete(true)]
public string getHello()
{//2nd breakpoint
ContextUtil.SetComplete();
return "HelloWorld";
}
}
I have another test project from which I call this component.
class Program
{
static void Main(string[] args)
{
MyComponent myComp = new MyComponent();
myComp.getHello();//1st Breakpoint
}
}
I am not able to reach 2nd Breakpoint. This was working before I switched to VS 2012. Strange thing is after switching to 2012 its no longer working in VS 2010 too.
I've already tried,
Attach to process
Unchecked "Enable Just My Code" in debug settings
Can someone please give direction from here?
UPDATE 1
From the links given by Mike, I tried symchk for my DLL in the same folder where DLL and PDB files were there. It fails with error saying PDB mismatched or not found. I don't know how to resolve this error.
You may be missing the .pdb file in your project.
Check this microsoft link out for an explanation: https://msdn.microsoft.com/en-us/library/yd4f8bd1(vs.71).aspx
I am trying to create a visual studio package, specifically I want to create a new menu option in the items context menu. As this one:
http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard
I already got the menu option display:
But now I want to configure the callback to create a file based on a template (custom visual studio template) like when we we click in Add > Class
But instead of use the class template, use one that I got created in my custom list. Avoiding the time to search the template in the list.
In the example that I follow to create the 'Add new service' buttom, in the first example is showed how to create a popup with:
IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
Guid clsid = Guid.Empty;
int result;
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
0,
ref clsid,
"NewService",
string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.ToString()),
string.Empty,
0,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
OLEMSGICON.OLEMSGICON_INFO,
0, // false
out result));
I guess that there should be more services to use with this VSPackages, but I don't found a method like that in the referece: https://msdn.microsoft.com/en-us/library/bb166217.aspx
Can you tell me from where I can found a method to perform this operation. Or how to archive my goal.
Update:
I am trying with this:
var dte = (DTE)GetService(typeof(DTE));
dte.ItemOperations.NewFile(#"General\Text File", "file.txt","7651A701-06E5-11D1-8EBD-00A0C90F26EA");
But i am getting this exception:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in NewService.dll but was not handled in user code
Additional information: Invalid class string Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING)
Solved!
I used the following piece of code:
private void MenuItemCallback(object sender, EventArgs e)
{
var dte = (DTE)GetService(typeof(DTE)) as EnvDTE80.DTE2;
var template = #"C:\Users\JuanAntonio\Documents\Visual Studio 2013\Templates\ItemTemplates\MyTemplate.vstemplate";
dte.Solution.Projects.Item(1).ProjectItems.AddFromTemplate(template, "Template.cs");
}
I have a problem that I am using coding4fun dll in my WP7 application for showing the popup messages.
I am using:
Micrsoft.Phone.Controls.Toolkit
Coding4fun.Phone.Controls
At first launch of deployment on device its crashing saying that value cannot be null(parameter name element) while on emulator its running fine. I have tried the latest version of this dll but the result was same.
While adding Micrsoft.Phone.Controls.Toolkit of latest version 1.4.8 is giving warning that adding a silverlight library may result in unexpected consequences.
while I tried other version of this dll still no success.
I am getting exception in stacktrace
Clarity.Phone.Extensions.DialogService.InitializePopUp
Clarity.Phone.Extensions.DilaogService.Show
Basically i am using that popup inside constuctor of mainpage.xaml(first page) after InitializeComponent() and it is throwing null reference type at first launch while deploying but app is getting installed. again if i run application on device then it is appearing correctly.
My code is:
notificationPrompt = new MessagePrompt();
notificationPrompt.Title = "Notification"
notificationPrompt.Body = "";
notificationPrompt.ActionPopUpButtons.Clear();
Button btnDisclaimer = new Button() { Content = "Yes" };
btnDisclaimerContinue.Click += new RoutedEventHandler(btnNotificationPromptYes_Click);
Button btnDisclaimerCancel = new Button() { Content = "No" };
btnDisclaimerCancel.Click += new RoutedEventHandler(btnNotificationPromptNo_Click);
notificationPrompt.ActionPopUpButtons.Add(btnDisclaimerContinue);
notificationPrompt.ActionPopUpButtons.Add(btnDisclaimerCancel);
notificationPrompt.Show();
I think it's the better to move all this code outside the constructor, and put it inside the Loaded event (occurs when a FrameworkElement has been constructed and added to the object tree: http://msdn.microsoft.com/en-us/library/ms596558(vs.95)) of the PhoneApplicationPage class, or just override the OnNavigatedTo method:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// What you want here...
...
}
Often when you have exceptions in the constructor of a PhoneApplicationPage, they will not show, making the debug more difficult and annoying...