473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Invoke

GTi
I have some functions that I want to Invoke in my C# project:

int NumberOfServers (BOOL UseENUM, LPCSTR MachineName);
BOOL GetServerName (int index, LPSTR Buffer, int BufSize);

can be used by:
[DllImport("some.dll")]
private static extern int NumberOfServers(bool UseENUM,
[MarshalAs(UnmanagedType.LPStr)]string MachineName);

[DllImport("some.dll")]
private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.LPTStr)]string Buffer, int BufSize);

But GetServerName just returns blanks:
string serverName = "
";
GetOPCServerName(index, serverName, serverName.Length);
And this one I have no idea:
HANDLE Connect(LPCSTR MachineName, LPCSTR ServerName, BOOL
EnableDLLBuffering);
HANDLE AddGroup(HANDLE hConnect, LPCSTR Name, DWORD *pRate, float
*pDeadBand);

And this one:
BOOL EnableNotification(HANDLE hConnect, NOTIFYPROC lpCallback);

void CALLBACK EXPORT OPCUpdateCallback(HANDLE hGroup, HANDLE hItem,
VARIANT *pVar, FILETIME timestamp, DWORD quality)
I know I must use delegates, but all help is wanted here.

Anyone ?

Nov 17 '05 #1
10 3027
GTi,

Your declaration of GetServerName is wrong. You need to use a
StringBuilder as opposed to a string when you expect the string to be
written to, like so:

[DllImport("some.dll")]
private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder Buffer, int BufSize);

As for the rest:

[DllImport("some.dll")]
static extern IntPtr(
[MarshalAs(UnmanagedType.LPTStr)]
string MachineName,
[MarshalAs(UnmanagedType.LPTStr)]
string ServerName,
bool EnableDLLBuffering);

[DllImport("some.dll")]
static extern IntPtr(
IntPtr hConnect,
[MarshalAs(UnmanagedType.LPTStr)]
string Name,
[MarshalAs(UnmanagedType.U4)]
ref int pRate,
ref float pDeadBand);

delegate void OPCUpdateCallback(
IntPtr hGroup,
IntPtr hItem
ref object pVar,
FILETIME timestamp,
[MarshalAs(UnmanagedType.U4)]
int quality);

[DllImport("some.dll")]
static extern EnableNotification(IntPtr, OPCUpdateCallback lpCallback);

You can get the definition of FILETIME from http://www.pinvoke.net, or
if you are using .NET 2.0, you can use the one in the
System.Runtime.InteropServices.ComTypes namespace.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

And this one:
BOOL EnableNotification(HANDLE hConnect, NOTIFYPROC lpCallback);

void CALLBACK EXPORT OPCUpdateCallback(HANDLE hGroup, HANDLE hItem,
VARIANT *pVar, FILETIME timestamp, DWORD quality)
I know I must use delegates, but all help is wanted here.

"GTi" <tu****@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...I have some functions that I want to Invoke in my C# project:

int NumberOfServers (BOOL UseENUM, LPCSTR MachineName);
BOOL GetServerName (int index, LPSTR Buffer, int BufSize);

can be used by:
[DllImport("some.dll")]
private static extern int NumberOfServers(bool UseENUM,
[MarshalAs(UnmanagedType.LPStr)]string MachineName);

[DllImport("some.dll")]
private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.LPTStr)]string Buffer, int BufSize);

But GetServerName just returns blanks:
string serverName = "
";
GetOPCServerName(index, serverName, serverName.Length);
And this one I have no idea:
HANDLE Connect(LPCSTR MachineName, LPCSTR ServerName, BOOL
EnableDLLBuffering);
HANDLE AddGroup(HANDLE hConnect, LPCSTR Name, DWORD *pRate, float
*pDeadBand);

And this one:
BOOL EnableNotification(HANDLE hConnect, NOTIFYPROC lpCallback);

void CALLBACK EXPORT OPCUpdateCallback(HANDLE hGroup, HANDLE hItem,
VARIANT *pVar, FILETIME timestamp, DWORD quality)
I know I must use delegates, but all help is wanted here.

Anyone ?

Nov 17 '05 #2
GTi
Ahhh tx Nicholas,
After some testing in the mean time I get this to work also:

private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string MachineName, int
BufSize);
Don't what VBByRefStr is (Visual Basic Type) ?

GTi

Nov 17 '05 #3
GTi
I get an runtime error (int the win32 dll) when using it with
StringBuilder:
[DllImport("some.dll")]
private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder Buffer, int BufSize);

public string ServerName(int index)
{
StringBuilder text = new StringBuilder("", 128);

try { GetServerName(index, text, text.MaxCapacity); }
catch (Exception e) { _lastError = e.Message; }
return (text.ToString());
}

Any idea why?

Nov 17 '05 #4
GTI,

You should not be using ref string, rather, StringBuilder is what is
guaranteed to work. The fact that it works with ref string is not a
supported mechanism to do what you are trying to do, and could change in the
future.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"GTi" <tu****@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Ahhh tx Nicholas,
After some testing in the mean time I get this to work also:

private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string MachineName, int
BufSize);
Don't what VBByRefStr is (Visual Basic Type) ?

GTi

Nov 17 '05 #5
GTi,

It's not working because you are passing MaxCapacity in for your buffer
size. This value, when used with a StringBuilder as you created it, will be
0x7fffffff, which is a really big number.

You should pass in the capacity of the string, returned by the Capacity
property.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"GTi" <tu****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I get an runtime error (int the win32 dll) when using it with
StringBuilder:
[DllImport("some.dll")]
private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder Buffer, int BufSize);

public string ServerName(int index)
{
StringBuilder text = new StringBuilder("", 128);

try { GetServerName(index, text, text.MaxCapacity); }
catch (Exception e) { _lastError = e.Message; }
return (text.ToString());
}

Any idea why?

Nov 17 '05 #6
GTi
Thanks Nicholas,
I have changed it now to:

[DllImport("some.dll", EntryPoint = "GetServerName", CharSet =
CharSet.Ansi)]
private static extern bool GetServerName(int index, [MarshalAs(
UnmanagedType.LPTStr)] StringBuilder Buffer, int BufSize);

StringBuilder text = new StringBuilder(128);
try { GetServerName(index, text, text.Capacity); }
catch (Exception e) { lastError = e; }
return (text.ToString());

But now it just return ????????????? where the name shuld be. But the
length of the ???? string (looks like it) match the real names.

Any tips?

Nov 17 '05 #7
GTi,

You should change your declaration to this (if you are sure that you are
using the Ansi character set:

[DllImport("some.dll", EntryPoint = "GetServerName", CharSet =
CharSet.Ansi)]
private static extern bool GetServerName(int index,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder Buffer, int BufSize);

This should work.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"GTi" <tu****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks Nicholas,
I have changed it now to:

[DllImport("some.dll", EntryPoint = "GetServerName", CharSet =
CharSet.Ansi)]
private static extern bool GetServerName(int index, [MarshalAs(
UnmanagedType.LPTStr)] StringBuilder Buffer, int BufSize);

StringBuilder text = new StringBuilder(128);
try { GetServerName(index, text, text.Capacity); }
catch (Exception e) { lastError = e; }
return (text.ToString());

But now it just return ????????????? where the name shuld be. But the
length of the ???? string (looks like it) match the real names.

Any tips?

Nov 17 '05 #8
GTi
Nicholas,
Was using
MarshalAs(UnmanagedType.LPTStr)
changed it to:
MarshalAs(UnmanagedType.LPStr)

and voila !
You are my hero !

GTi

Nov 17 '05 #9
GTi
OK - I don't remeber it now - I give up.
Simple question:

How can I referer to FILETIME to the
System.Runtime.InteropServices.ComTypes namespace instead of the
System.Runtime.InteropServices ?

delegate void OPCUpdateCallback(IntPtr hGroup, IntPtr hItem, ref object
pVar, FILETIME timestamp, [MarshalAs(UnmanagedType.U4)]int quality);
Gives this error:
'FILETIME' is an ambiguous reference between
'System.Runtime.InteropServices.FILETIME' and
'System.Runtime.InteropServices.ComTypes.FILETIME'

But I can't remember what the syntax is to referer to another
namespace:
System.Runtime.InteropServices.ComTypes.FILETIME timespamp
does not work at all....

Nov 17 '05 #10
GTi
I solved it (rare bug)

My name space name is:
namespace MyLib.System

So trying to spesify the correct type using:
System.Runtime.InteropServices.ComTypes.FILETIME timestamp
gives me an error saying that Runtime was unknown..

Renaming my namespace to:
namespace MyLib.Lib
solved the problem....

(And I was almost giving up C# for this!)

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Tom | last post by:
Hi Everybod I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
14
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.