|
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
|
|
|
|
|
I've a problem that I've been struggaling with for a couple of weeks. I've made a client sever application which transfer files between the server and the client. The link between the server and the client only allows multicast so this has been done with UDP/IP. The applications that uses this connection needs TCP/IP. The solution to the problem must be a TCP/IP packet encasulated in a UDP/IP packet. I'm trying to do this in C# and hopefully have someone can help me out.
Thanks in a million
Eric
|
|
|
|
|
C# is too high a level programming language for something like this. Your talking about writing a comm driver and that is best suited to C++.
Now, you're going to have a bit of problem transmitting TCP in a UDP packet. UDP doesn't guarantee delivery of packets and does not even guarantee delivery of packets in the correct order, whereas TCP does guarantee this. You'll have to devise a mechanism to reassemble that TCP packets and then request a retransmission of the missing packets.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
hey friends,
I am working on the isobar(contour Line) i have a collection of X axis and Y axis and Some Pressure
values, in which some of them may same. can some give me a contouring algorithm and some idea so that i can draw the isobar
thanks
beolijza
|
|
|
|
|
I have just started with C# and porting a C++ application. In C++, I can augment a derived class function like the code below:
void DerivedClass::Foo ()
{
/*
Do some processing…
*/
BaseClass::Foo ();
}
How can I achieve this in C#? I found the below idea as a working solution, but then it does not allow me to override the Foo() function, it advices to have a ‘new’ implementation.
public new string Foo()
{
BaseClass oBaseClass = this as BaseClass;
return oBaseClass.Foo();
}
Can somebody explain the theory to me please...
|
|
|
|
|
To call the base implementation from a derived class use: base.Foo()
The as keyword is a type of cast operator, where null is returned if the case fails. The other type of cast operator (class_name_here) will throw an exception if the cast fails.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
|
|
|
|
|
Do you mean to say, I need to *instantiate* a base class object and then call the foo() method on it? That is too much of a trouble as compared to base::foo() way of C++
|
|
|
|