|
This would be too good to be true. I thought now i functions because now with your hint the MethodMissingException does not occur again. But when i execute now my program with the emulator, nothing happens..?? But when i defined a normal empty project this code will work. The code fragment look as follows: (probably you know what went wrong here - i have to say slowly i' ll get depressed..)
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
namespace SmartDeviceApplication1
{
class Test
{
static AutoResetEvent evt;
static int count = 5;
static void Main(string[] args)
{
if (args.Length > 0)
{
try
{
count = int.Parse(args[0]);
}
catch (Exception e)
{
e.ToString();
}
}
evt = new AutoResetEvent(false);
Timer t = new Timer(new TimerCallback(TimerCallback), null, 0, 5000);
evt.WaitOne();
}
static void TimerCallback(object state)
{
Console.WriteLine("Enumerating windows at {0:t}...", DateTime.Now);
string title = new string('\0', 260);
int ret = GetWindowText(GetForegroundWindow(), title, 260);
if (ret != 0)
System.Console.WriteLine(title.Substring(0, ret));
}
[DllImport("coredll.dll", CharSet=CharSet.Auto)]
static extern int GetWindowText(IntPtr hWnd, [Out] string title,
int maxCount);
[DllImport("coredll.dll")]
private static extern IntPtr GetForegroundWindow();
}
}
Hopefully you can help mne?!
Regards
pat
|
|
|
|
|
The code looks as follows:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
namespace SmartDeviceApplication1
{
class Test
{
static AutoResetEvent evt;
static int count = 5;
static void Main(string[] args)
{
if (args.Length > 0)
{
try
{
count = int.Parse(args[0]);
}
catch (Exception e)
{
e.ToString();
}
}
evt = new AutoResetEvent(false);
Timer t = new Timer(new TimerCallback(TimerCallback), null, 0, 5000);
evt.WaitOne();
}
static void TimerCallback(object state)
{
Console.WriteLine("Enumerating windows at {0:t}...", DateTime.Now);
string title = new string('\0', 260);
int ret = GetWindowText(GetForegroundWindow(), title, 260);
if (ret != 0)
System.Console.WriteLine(title.Substring(0, ret));
}
[DllImport("coredll.dll", CharSet=CharSet.Auto)]
static extern int GetWindowText(IntPtr hWnd, [Out] string title,
int maxCount);
[DllImport("coredll.dll")]
private static extern IntPtr GetForegroundWindow();
}
}
Do you know why this program does anyway not execute correctly on the PocketPc-Emulator?
Regards
pat
|
|
|
|
|
Hello,
I have 2 forms and l want to be able to copy text inputs from one form to the other form. In Visual Basic this was very easy to do. e.g. txtStudentID.Text = frmNewStudent.txtNewStudentID.text.
But i am not sure how to do this in C#.
Can any one tell me.
Thanks in advance,
Steve
|
|
|
|
|
if your TextBox variables are public and not private, then you should be able to do the same things that you did in VB. If you go to the .cs file for the form you should see the TextBox variables defined at the top of the form.
As an alternative, you could make some properties that get and set the data for the other form. This is a more object oriented approach, but not always the best for every application.
Steve Maier, MCSD MCAD
|
|
|
|
|
This is object-oriented development. You need to ensure that objects can access easy other. In this case, make sure that one form can access another, either by passing a variable to a method (perhaps the constructor), assigning a property, or have some intermediary that can access both form objects.
Now, Steve was talking about encapsulation and that is a good idea. Exposing the actual TextBox es to outside code is not (almost never is) a good idea. Remember that those TextBox es have a lot more properties that could be abused.
So, you could simply encapsulate the Text property on your form class (remember that the property would be defined on your form class and not Form , so you may have to cast to your type depending on how one form accesses another):
public string StudentID
{
get { return txtStudentID.Text; }
set { txtStudentID.Text = value; }
} If you don't want this publicly accessible and the forms are not derivative of each other, you can use the internal access modifier, which means that any code within that particular assembly (.NET 2.0 defines an attribute that grants other assemblies that you specify access to internals as well) can access that property (like public only for that assembly, but private to other assemblies).
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
Hello,
Thanks for you reply.
I think l will do it the way that you said.
Just another question, l am creating a small database application for a small library system. I have 2 tables books and students.
The properties of students are: StudentID, Name, Address, Email. I have never done a database application using c#. Just on the student table, how can l make that oop, for example adding, updating and deleting.
I find it easy to do it the vb way.
Any example programs or tutorials.
Many thanks in advance.
Steve
|
|
|
|
|
From the IDE bring up the help files / Index tab.
In the index search for SQLDataAdapter
In the list of help topics is samples. This also exposes the various methods you can use for accessing a database. Access is much much easier than it ever was.
This signature left intentionally blank
|
|
|
|
|
I have been working on a class that will automate an instance of internet explorer based on a script file. The script file will be generated by a non-MFC win32 application written in c++. This application will then need to access my automation classes (could be as simple as starting an executable and passing a file name as argument). I have the basics of my class written and working in managed c++ and c#. The c# code is by far the cleanest looking, and the yet-to-be-done unmanaged c++ will be the ugliest for sure. I am hoping to get advice as to which language and what project type to use.
unmanaged c++ is most familiar (but I really dont want to do this), and I would likely create a .dll, or possibly even a static library.
managed c++ is fairly familiar, but I am not sure what type of project I would use
c# I am new to, but like what I have seen so far. I am leaning toward this, but am not sure if I can access a c# library from unmanaged code, if it should be a console application (I would need to hide the console window), a windows application with no visible window, or possibly a service.
The overall goal is to call some code with an automation script, and this code will take it from there. At this point there is no need for any communication back to the calling application, but it would be nice to have this option left open.
Thank You
|
|
|
|
|
I did something similar in a project for my last job. There were two approaches that we took. The first was to expose the .Net (C#) object with a COM interface so that unmanaged code could use the objects like a normal COM object. The second way is to compile your unmanaged code to include the managed extensions. Then you can just create a new variable of the class type that is in the C# DLL that you make and just use it.
I prefer the second method, but there are a few things that you have to do to be able to do this. I only have VS 2005 installed here now, so this is from memory and a small test that I did here.
- Turn on the managed extensions in the project. This is probably in Configuration Proerties labeled something like "Common Language runtime support"
- Add the following code to your stdafx.h or similar file.
#using < mscorlib.dll >
using namespace System;
using namespace System::Runtime::InteropServices;
- Add the reference to your C# DLL to the unmanged project
- Create a manged variable using code like this:
gcroot< ClassLibrary1::Class1* > c = new ClassLibrary1::Class1();
int t=c->MyProperty;
CString buff;
buff.Format("data = %d",t);
MessageBox(buff);
c->ShowIt();
- Use the class instance using a ->
Using this method you can make the C# library just be a class library project and call the classes from it in your unmanaged code.
Steve Maier, MCSD MCAD
|
|
|
|
|
I should have added the following information:
Our current unmanaged application runs, and needs to continue to run on systems without the .NET framework. I planned on doing a test at the time the new functionality is required to see if the framework is installed, and if not just displaying a message box. I guess this leads me more toward you first solution. I have some limited familiarity with COM-ATL so I could probably work that into our existing code. How would I go about exposing my c# object with a COM interface?
Thank You,
|
|
|
|
|
I have found this article...I should have googled before replying
Thank You
http://www.csharphelp.com/archives/archive190.html
|
|
|
|
|
That is definately a good start. You might also want to put a GUID Attribute above your class definition. Then it will look just like other COM objects.
[GuidAttribute("9ED54F84-A89D-4fcd-A854-44251E925F09")]
class SampleClass{
} I have not played with the idea before, but it might be that if you do this then you might be able to use the fact of the object not initializing to tell you that the framework is not there or that there is something not installed right.
Steve Maier, MCSD MCAD
|
|
|
|
|
I could even go with something like this
http://www.codeproject.com/dotnet/bridge.asp
would be nice to avoid COM altogether
|
|
|
|
|
I use a web application config file in my usercontrol, then I put it in one web application. But the config file can't read from the web application. How do it?
|
|
|
|
|
You do not need a config file just for your usercontrol. Remove it, and place the settings inside of the application config file.
my blog
|
|
|
|
|
Hi~~
I have a problem transfer byte array from directshow filter to C# application.
I made simple directshow filter using C++. that filter has a interface to communication outside application. like this..
DECLARE_INTERFACE_(IGetCurrentData, IUnknown)
{
STDMETHOD(Get_Current_Media) (THIS_
BYTE** pData
) PURE;
STDMETHOD(Get_Current_Media_Legnth) (THIS_
long& length
) PURE;
};
I also declare interface in C# application to communicate with filter(C++ Dll) like this..
[ComVisible(true), ComImport,
Guid("CCB4EF12-C1CC-43a4-82EF-7886C0F39EB1"),
InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
public interface IGetCurrentData
{
[PreserveSig]
int Get_Current_Media([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]out byte[]ar);
[PreserveSig]
int Get_Current_Media_Legnth(out long length);
}
I use this sentence to use C# interface.
byte[] pData;
Get_Current_Media(out pData);
but I meet an exception during runtime. I think datatype is not match. How can I solve this problem?
|
|
|
|
|
|
many thanks for your response. but I have still problem.
I also find this answer. but this solution is a little bit
different with my problem.
In this solution, C++ use C# dll. but in my case
C# use C++ dll.
Maybe this solution is correct. but I can't solve my
problem....
please teach me...
thank you.
C++:
long *ar = new long[8192];
for (i=0; i<8192; i++)
ar[i] = i;
managed->SetArrayElements(8192, ar);
C#:
public void SetArrayElements(int len,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]int[]
ar)
{
int[] bbb = new int[10240];
Array.Copy(ar, 0, bbb, 0, len);
Console.WriteLine("Length= {0}, ar[0] = {1}",
ar.Length, ar[0]);
}
--------
|
|
|
|
|
Sorry, your topic said C++ to C#, but I think you're saying you want C# to C++. In that case, instead of using a byte *, try switching to VARIANT and SAFEARRAY. SAFEARRAY can be casted right to an CArray I believe. If you don't have control over the C++ interface, I'm not sure what to do to be quite honest with you.
Custom Software, Custom Solutions. Yye Software.
http://www.yyesoftware.com
|
|
|
|
|
An unmanaged long is 32 bits, not 64; therefore, an int (System.Int32 ) is the correct type to marshal to your COM object.
Also, your SizeParamIndex isn't valid. Your array is passed in parameter 0, not your size. You actually shouldn't even need to use the MarshalAsAttribute here. A byte[] array is already a reference type (a managed pointer, if you will), and passing that with out yields a pointer to your pointer, just like a BYTE** (or LPBYTE* ). Without declaring SizeParamIndex or SizeConst , however, you'll run into a problem.
When marshalling from unmanaged to managed code (or even just passing data in unmanaged code with or without marshalling), there's no way to infer the size of an array (of whatever data type). If this is an interface you declared, you really should define another parameter that returns the number of bytes in the array - not just for the sake of .NET - but for any callers (unless the size is always known, in which case you should use the SizeConst field in your MarshalAsAttibute ).
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
thanks for your reply. I solve that propbem.
as you said to me, I use that statement...like this..
int Get_Current_Media([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]out byte[]ar, out int len);
My C++(dll) module can communicate with c# application perfectly...!!!
but speed is so late. approximately spend 0.5 second.
So I can't use this method in my c# application. I have a
plan to remake application using c++ -_-
Anyway I am really thankful to your answer.
|
|
|
|
|
|
You could, however, declare overloads:
public void MyMethod(int a)
{
MyMethod(a, 3);
}
public void MyMethod(int a, int b)
{
}
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
I created a simple user control in C# which has a button in it.
//DemoControl.cs
namespace DemoControl
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button button1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// UserControl1
//
this.Controls.Add(this.button1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(608, 248);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseMove);
this.ResumeLayout(false);
}
#endregion
}
}
The i created an MFC ActiveX control to host the C# control
//HostCtrol.cpp
int CMfcHostCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
AfxEnableControlContainer();
DemoControl::UserControl1* pcal = new DemoControl::UserControl1();
CComPtr<iunknown> spunkControl;
spunkControl.Attach((IUnknown*)System::Runtime::InteropServices::Marshal::GetIUnknownForObject(pcal).ToPointer());
CRect rectPlaceHolder;
GetClientRect( &rectPlaceHolder );
m_ctrl.Create(spunkControl, WS_VISIBLE | WS_TABSTOP, rectPlaceHolder, this, 0);
return 0;
}
where m_ctrl is CWinFormsControlWnd class which i downloaded from the MSDN Magazine at http://msdn.microsoft.com/msdnmag/issues/03/03/WindowsForms/default.aspx
It works great, but the hosting application stuck when i press the button.
Does anyone knwos whats wrong?
Amir.
Amir Harel
|
|
|
|
|
Hi
I am looking for component or some solution how to write databound list which allows:
1. enter SQL query as source
2. paging (datasource may contain more than 100000 records...)
3. master-detail view
4. customising filter (on almost every field)
5. filtering master rows based on some detail elements
6. storing filter settings (serializable) to file or db
7. filtering on database not in dataset
Do I really need to write it on my own?
ooops... I forget to say - WinForms of course
|
|
|
|
|