|
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++
|
|
|
|
|
|
_l_Salil_l_ wrote:
Do you mean to say, I need to *instantiate* a base class object and then call the foo() method on it?
No.
class A
{
virtual void Foo(){}
}
class B:A
{
override void Foo()
{
base.Foo();
}
}
"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!
|
|
|
|
|
Thanks... It took a while to make out that 'base' is a C# keyword.
|
|
|
|
|
Hi,
I have 5 years experience in VC++, MFC and C++. Now I want to do programming in .Net framework. Which is best among C#, VB.Net and VC++ 7.
Which books should I refer for C#?
Thanks,
Vicky
|
|
|
|
|
Vicks wrote:
Which is best among C#, VB.Net and VC++ 7
That's a difficuly question without more information.
Since you've got a C++ background, I'd drop VB.NET from the list and concentrate on examining C# Vs. Managed C++ (I'm assuming you actually meant Managed C++ and not "normal" VC++)
If you are going to do a lot of interop work the MC++ is probably better. If not then I'd go with C#. C# is fairly close to C++ syntax. There are some differences (like the struct keyword has different meanings)
Vicks wrote:
Which books should I refer for C#?
Looks like you've already chosen C# anyway. There are lots of good books on the subject. I bought mine 2+ years ago so there are probably better introductory books by now. However I found "Applied Microsoft .NET Framework programming" by Jeffrey Richter to be a good source of information.
"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!
|
|
|
|
|
There is not a declared ‘best’ and people tend to rate them based on their own likings. In .net code written in any language ultimately gets executed in an IL on the CLR. So one should choose ‘his’ language of choice in .Net based on
a. The past experience
b. The nature of applications to be developed
I come with a similar tech background as yours and I found C# to be a more natural progression of my skill set. C++.net was also an alternative, but it will be best suited if I'm into lot of unmanaged code and/or want a quicker port of the older code. For fresh projects, C# is recommended.
|
|
|
|
|
hi,
Professional C# 2 nd Edition by Wrox is good.
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
|
I was in your boat a couple of years back. If you have 5 solid years of C++ (meaning using it everyday), then C# is going to be a piece of cake. Like you, I'm a book learner and immediately thought about getting several books. I did pick up a few here and there, but most were specialized in areas that I wanted to learn (remoting, GUI, etc). I think someone with your skill set should start with the help files & Intellisense, you'll get bored with the books. What I did was come up with a project to write (in my case, it was a code snippet manager) and went for it. Just like any project, you'll come up against problems to solve and this will solidify your knowledge and allow you to begin building a code base/libraries for yourself. You really know more about C# than you think because it is so similar to C++ and you know most of the framework because of your MFC knowledge. It's just like MFC, only better and easier.
Just my opinion.
Custom Software, Custom Solutions. Yye Software.
http://www.yyesoftware.com
|
|
|
|