|
ah. just found the Marshal-class - maybe this helps me...
:wq
|
|
|
|
|
First of all, are you sure about the signature of your C method ?
Isn't it more like this :
int foo(void **ppMessage);
To expose this C function to managed code, you need to declare it as :
[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
public static extern int foo(out IntPtr ppMessage);
where user32.dll must be replaced with the actual DLL name.
And I swallow a small raisin.
|
|
|
|
|
erm no, i looked it up in the header-file and there's only one star. i'm a bit confused too, but it seems you have to know, that you get a pointer in the specified location.
actually i have solved the thing like this:
[DllImport("theold.dll")]
public static extern Int32 foo(IntPtr [] messagePtr);
...
IntPtr [] messagePtr = new IntPtr[1];
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr[0]);
byte [] message = new byte[size];
Marshal.Copy(messagePtr[0],message,0,size);
i know (from the specification) that the first 2 bytes contain the size of the result in bytes.
:wq
|
|
|
|
|
Hi, it seems you should be able to remove the array like:
[DllImport("theold.dll")]public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,2,size);
I'm interested, does this work too?
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
1. yep. out works indeed. good idea. but in the foo-call is to be inserted uswell.
2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes)
finally:
[DllImport("theold.dll")]
public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(out messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,0,size);
thx again for your help an ideas
:wq
|
|
|
|
|
Rüpel wrote:
but in the foo-call is to be inserted uswell.
oops , happens when u write the code out of your head straight into the textbox
Rüpel wrote:
2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes)
But wont the fist 2 byte values (being a size value) affect the byte[]?
(oops another typo , meant to do this)
byte [] message = new byte[size - 2];
Marshal.Copy(messagePtr,message,2,size);
or
byte [] message = new byte[size - 2];
Marshal.Copy(new IntPtr(messagePtr.ToInt32() + Marshal.SizeOf(typeof(byte))*2),message,0,size - 2);
I'm just interested
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
I want to know wheather we can write components using c# in place of ATL COM.i.e..is it possible to write everything whatever we do in ATLCom.
|
|
|
|
|
It depends if you intend to consume these components from unmanaged or managed code.
If that's managed code, then you are talking about the whole .NEt platform. A C# user control exposes interfaces that can be used in a VB.Net class.
If that's unmanaged code :
You'd better have good reasons to do so. ATL/COM components do not require the .Net framework to work.
And yes, there are tutorials about this in MSDN. You usually end up by doing a tlbexp.exe or regasm.exe (which is the opposite of importing a COM type-library).
And I swallow a small raisin.
|
|
|
|
|
If I want my AP be only one ? How can I do this in C#?
There is an example in VC http://www.codeproject.com/threads/singleinstance.asp
|
|
|
|
|
VC apps usually take advantage of mutexes. Go in the .Net framework MSDN doc, search "Mutex". Finish the implementation.
And I swallow a small raisin.
|
|
|
|
|
When I saw this, I immediately thought of 'Highlander'.....
Christian
We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum )
Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
|
|
|
|
|
|
|
What do you mean by "doesn't like"?
Paul
|
|
|
|
|
it does not compile, as the function definition is as follows.
ChangeDisplaySettings(ref DEVMODE d, int flags);
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
DEVMODE is a struct.
1. just make another definition
ChangeDisplaySettings(object dummy, int flags);
or
2. change DEVMODE to a class with a sequential layout. Then you can pass null.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
|
I sent you VS solution, that happened to me too, seemed to be due not changing the enum declaration from ref DEVMODE to just DEVMODE ,its a class now so we dont need to. I however had to add the OutAttribute the the declaration.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
How about making the function unsafe ?
Then use a pointer as parameter.
But that's maybe not possible when it's an imported function!?
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
have geen trying to get he current displaymode but have been unable, my latest attmpt was to try[^] and convert this articles code, but i had a little difficulty. I found the content of the DEVMODE struct on MSDN and put it into my code. I declare the EnumDisplaySettings like this
[DllImport("User32.dll")]<br />
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, DEVMODE lpDevMode);
then use it in code like this:
DEVMODE dm = new DEVMODE();
EnumDisplaySettings(null, -1, dm); but i get the exception 'object reference not set to an instance of an object' yet clearly, a DEVMODE object has been initiated.
any help would be appreciated.
*sudden thought* in the decleration of the DEVMODE struct, on MSDN, a couple of the members where of tpye TCHAR , declared like this.
TCHAR dmDeviceName[CCHDEVICENAME];<br />
TCHAR dmFormName[CCHFORMNAME];
i couldnt find what CCHFORMNAME is so i just used the MarshalAs Attribute like this
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string dmDeviceName; for both, could that be the problem?
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
I think the exception lies with string lpszDeviceName being null.
You mite want to redefine:
[DllImport("User32.dll")]
public static extern bool EnumDisplaySettings(StringBuidler lpszDeviceName, int iModeNum, DEVMODE lpDevMode);
Then when calling do StringBuilder devname = new StringBuilder( insert const size here).
Also , many times you need to set the size of a struct. In this case, according to MSDN, you will need to.
Nnamdi Onyeyiri wrote:
i couldnt find what CCHFORMNAME is so i just used the MarshalAs Attribute like this
This you will find the value in the header files, so download the Platform SDK if you dont have it already.
Hope this helps
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
leppie wrote:
I think the exception lies with string lpszDeviceName being null.
i dont think so, i tried entering a value for that, and it did not work, it gavethe same exception. i used null, because that is what was used in the article i gave the link for.
leppie wrote:
This you will find the value in the header files, so download the Platform SDK if you dont have it already.
is that installed with the default settings for vs.net install?
leppie wrote:
Also , many times you need to set the size of a struct. In this case, according to MSDN, you will need to.
i didnt know about this, ill have a look now.
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
Nnamdi Onyeyiri wrote:
dont think so, i tried entering a value for that, and it did not work, it gavethe same exception. i used null, because that is what was used in the article i gave the link for.
Still try using StringBuilder as u CAN pass null as a value.
Nnamdi Onyeyiri wrote:
is that installed with the default settings for vs.net install?
If u install VC++ , else just look for a dir in program files with many *.h files
I had a look and that is a pretty viscious struct . Im not sure what a BCHAR is though. Im gonna try do something in C# and see what i come up with. Can you perhaps mail me what u have done so far?
Cheers
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
|
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
int mode = 0;
while (EnumDisplaySettings(null, mode++, ref dm) != 0)
{
Console.WriteLine("{0}x{1}, {2}bits {3}hz",
dm.dmPelsWidth, dm.dmPelsHeight, dm.dmBitsPerPel, dm.dmDisplayFrequency);
}
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
static extern int EnumDisplaySettings(StringBuilder devname, int mode, ref DEVMODE devmode);
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
This works Note the union ???? looks like it combines 2 int 's ???
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|