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(Unman agedType.LPStr)]string MachineName);
[DllImport("some .dll")]
private static extern bool GetServerName(i nt index,
[MarshalAs(Unman agedType.LPTStr )]string Buffer, int BufSize);
But GetServerName just returns blanks:
string serverName = "
";
GetOPCServerNam e(index, serverName, serverName.Leng th);
And this one I have no idea:
HANDLE Connect(LPCSTR MachineName, LPCSTR ServerName, BOOL
EnableDLLBuffer ing);
HANDLE AddGroup(HANDLE hConnect, LPCSTR Name, DWORD *pRate, float
*pDeadBand);
And this one:
BOOL EnableNotificat ion(HANDLE hConnect, NOTIFYPROC lpCallback);
void CALLBACK EXPORT OPCUpdateCallba ck(HANDLE hGroup, HANDLE hItem,
VARIANT *pVar, FILETIME timestamp, DWORD quality)
I know I must use delegates, but all help is wanted here.
Anyone ? 10 3065
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(i nt index,
[MarshalAs(Unman agedType.LPTStr )] StringBuilder Buffer, int BufSize);
As for the rest:
[DllImport("some .dll")]
static extern IntPtr(
[MarshalAs(Unman agedType.LPTStr )]
string MachineName,
[MarshalAs(Unman agedType.LPTStr )]
string ServerName,
bool EnableDLLBuffer ing);
[DllImport("some .dll")]
static extern IntPtr(
IntPtr hConnect,
[MarshalAs(Unman agedType.LPTStr )]
string Name,
[MarshalAs(Unman agedType.U4)]
ref int pRate,
ref float pDeadBand);
delegate void OPCUpdateCallba ck(
IntPtr hGroup,
IntPtr hItem
ref object pVar,
FILETIME timestamp,
[MarshalAs(Unman agedType.U4)]
int quality);
[DllImport("some .dll")]
static extern EnableNotificat ion(IntPtr, OPCUpdateCallba ck 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.co m And this one: BOOL EnableNotificat ion(HANDLE hConnect, NOTIFYPROC lpCallback);
void CALLBACK EXPORT OPCUpdateCallba ck(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.c om> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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(Unman agedType.LPStr)]string MachineName);
[DllImport("some .dll")] private static extern bool GetServerName(i nt index, [MarshalAs(Unman agedType.LPTStr )]string Buffer, int BufSize);
But GetServerName just returns blanks: string serverName = " "; GetOPCServerNam e(index, serverName, serverName.Leng th);
And this one I have no idea: HANDLE Connect(LPCSTR MachineName, LPCSTR ServerName, BOOL EnableDLLBuffer ing); HANDLE AddGroup(HANDLE hConnect, LPCSTR Name, DWORD *pRate, float *pDeadBand);
And this one: BOOL EnableNotificat ion(HANDLE hConnect, NOTIFYPROC lpCallback);
void CALLBACK EXPORT OPCUpdateCallba ck(HANDLE hGroup, HANDLE hItem, VARIANT *pVar, FILETIME timestamp, DWORD quality) I know I must use delegates, but all help is wanted here.
Anyone ?
Ahhh tx Nicholas,
After some testing in the mean time I get this to work also:
private static extern bool GetServerName(i nt index,
[MarshalAs(Unman agedType.VBByRe fStr)]ref string MachineName, int
BufSize);
Don't what VBByRefStr is (Visual Basic Type) ?
GTi
I get an runtime error (int the win32 dll) when using it with
StringBuilder:
[DllImport("some .dll")]
private static extern bool GetServerName(i nt index,
[MarshalAs(Unman agedType.LPTStr )] StringBuilder Buffer, int BufSize);
public string ServerName(int index)
{
StringBuilder text = new StringBuilder(" ", 128);
try { GetServerName(i ndex, text, text.MaxCapacit y); }
catch (Exception e) { _lastError = e.Message; }
return (text.ToString( ));
}
Any idea why?
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.co m
"GTi" <tu****@gmail.c om> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com... Ahhh tx Nicholas, After some testing in the mean time I get this to work also:
private static extern bool GetServerName(i nt index, [MarshalAs(Unman agedType.VBByRe fStr)]ref string MachineName, int BufSize); Don't what VBByRefStr is (Visual Basic Type) ?
GTi
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.co m
"GTi" <tu****@gmail.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. . I get an runtime error (int the win32 dll) when using it with StringBuilder:
[DllImport("some .dll")] private static extern bool GetServerName(i nt index, [MarshalAs(Unman agedType.LPTStr )] StringBuilder Buffer, int BufSize);
public string ServerName(int index) { StringBuilder text = new StringBuilder(" ", 128);
try { GetServerName(i ndex, text, text.MaxCapacit y); } catch (Exception e) { _lastError = e.Message; } return (text.ToString( )); }
Any idea why?
Thanks Nicholas,
I have changed it now to:
[DllImport("some .dll", EntryPoint = "GetServerName" , CharSet =
CharSet.Ansi)]
private static extern bool GetServerName(i nt index, [MarshalAs(
UnmanagedType.L PTStr)] StringBuilder Buffer, int BufSize);
StringBuilder text = new StringBuilder(1 28);
try { GetServerName(i ndex, 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?
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(i nt index,
[MarshalAs(Unman agedType.LPStr)] StringBuilder Buffer, int BufSize);
This should work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"GTi" <tu****@gmail.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. . Thanks Nicholas, I have changed it now to:
[DllImport("some .dll", EntryPoint = "GetServerName" , CharSet = CharSet.Ansi)] private static extern bool GetServerName(i nt index, [MarshalAs( UnmanagedType.L PTStr)] StringBuilder Buffer, int BufSize);
StringBuilder text = new StringBuilder(1 28); try { GetServerName(i ndex, 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?
Nicholas,
Was using
MarshalAs(Unman agedType.LPTStr )
changed it to:
MarshalAs(Unman agedType.LPStr)
and voila !
You are my hero !
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 OPCUpdateCallba ck(IntPtr hGroup, IntPtr hItem, ref object
pVar, FILETIME timestamp, [MarshalAs(Unman agedType.U4)]int quality);
Gives this error:
'FILETIME' is an ambiguous reference between
'System.Runtime .InteropService s.FILETIME' and
'System.Runtime .InteropService s.ComTypes.FILE TIME'
But I can't remember what the syntax is to referer to another
namespace:
System.Runtime. InteropServices .ComTypes.FILET IME timespamp
does not work at all.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 has been made.
Whenever I attempt to update my frmProgress, the events
are put placed at the end of the thread's queue and
executed after my algorithm is finished. Using the
frmProgress.Label.Invoke method with delagates, I have
heard would fix...
|
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
function, which in turn fires an event that is caught by the form.
The form then need to add a value passed in the event to a form object,
however, when using the Invoke method, it just hangs.
|
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 for all of my controls in the Form and dont define one delegate for each control. Some thing like to have a switch case in that delegate fucntion and passing control name to it..
what is the impact of this method in comparison with...
|
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 exe. The button clicking part is working fine, but I just
cannot get the method call.
Here is my program snippet.
**************************************************************
|
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 (and types), and with out parmeters. What I
want to do is to support each method call with exception (http 404,
soap exception, and other types of exceptions) and wrap it with try &
catch (a lots of catch ;-)
| |
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 waits
for the worker thread to complete by means of a while t.isAlive,
sleep(0) mechanism. But when my worker thread calls my
UpdateProgressBar routine, which calls Me.Invoke, the invoke call
blocks forever. But I can't figure out why the main...
|
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 EventHandler();
Invoke(d, new object{sender, e});
} else {
|
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, string s)
2. MyDelegate MyDelegateVar = MyMethod
3. MyForm.Invoke (MyDelegateVar)
Some questions:
|
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 or simple). I am hoping
the description will generate some ideas I can check out.
PROBLEM:
-----------------
Switching to UI thread using Invoke(), everything seems good.
|
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 code is:
Public Class MyClass1
Implements System.ComponentModel.ISynchronizeInvoke
Private _InvokeRequired As Boolean = False
Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |