powerShell error with Events and ref Arguments

This sample Script brings powerShell to crash because of the ref Argument in 
the C# event.
Does anybody have an idea how to solve the problem, when passing Events with 
ref arguments to a powerShell script. I just want to ask a script from a C# 
assemblie to get some values and not only one return value.


#Add new Class Type
Add-Type -TypeDefinition @'
    public class TestClass
    {
        public delegate void SetupEventHandler(ref int value);
//        public delegate void SetupEventHandler(int value);
        public event SetupEventHandler Setup;

        public void fireSetupEvent(int value)
        {
            if (Setup != null)
            {
				int newValue;
				newValue = value;
                Setup(ref newValue);
//                Setup(newValue);
            }
        }
    }
'@

#Function for the Event
function global:SetupEventFunction($EventArgs)
{
    Write-Host "EventFunction Start"
	Write-Host $EventArgs.SourceArgs.Count
    Write-Host ("Wert 0: {0}" -f $EventArgs.SourceArgs[0])
    Write-Host "EventFunction End"
}

$testObject = New-Object TestClass

#$setupEvent = Register-ObjectEvent -InputObject $testObject -eventName 
"Setup" -Action {SetupEventFunction($Event)}
$setupEvent = Register-ObjectEvent -InputObject $testObject -eventName 
"Setup" -Action {Write-Host "Hugo"}

[int]$myValue = 200

$testObject.fireSetupEvent($myValue)

0
Utf
6/30/2010 8:18:54 AM
windows.powershell 690 articles. 0 followers. Follow

1 Replies
1014 Views

Similar Articles

[PageSpeed] 47

Perhaps you know this I can't tell. The [ref] attributes is used to avoid the 
copying of an object in .NET/C++. In both cases adding the [ref] attribute 
increases overhead and adds nothing.

Perhaps this is a simplier example of what you're trying to do; again I 
can't tell

change this
> $testObject.fireSetupEvent($myValue)
to this
> $testObject.fireSetupEvent([ref]$myValue)

If that doesn't work I or someone can look at this further. 

btw didn't you get at least a warning that this parameter required a 
reference?

bob



"Jochen Axt" wrote:

> This sample Script brings powerShell to crash because of the ref Argument in 
> the C# event.
> Does anybody have an idea how to solve the problem, when passing Events with 
> ref arguments to a powerShell script. I just want to ask a script from a C# 
> assemblie to get some values and not only one return value.
> 
> 
> #Add new Class Type
> Add-Type -TypeDefinition @'
>     public class TestClass
>     {
>         public delegate void SetupEventHandler(ref int value);
> //        public delegate void SetupEventHandler(int value);
>         public event SetupEventHandler Setup;
> 
>         public void fireSetupEvent(int value)
>         {
>             if (Setup != null)
>             {
> 				int newValue;
> 				newValue = value;
>                 Setup(ref newValue);
> //                Setup(newValue);
>             }
>         }
>     }
> '@
> 
> #Function for the Event
> function global:SetupEventFunction($EventArgs)
> {
>     Write-Host "EventFunction Start"
> 	Write-Host $EventArgs.SourceArgs.Count
>     Write-Host ("Wert 0: {0}" -f $EventArgs.SourceArgs[0])
>     Write-Host "EventFunction End"
> }
> 
> $testObject = New-Object TestClass
> 
> #$setupEvent = Register-ObjectEvent -InputObject $testObject -eventName 
> "Setup" -Action {SetupEventFunction($Event)}
> $setupEvent = Register-ObjectEvent -InputObject $testObject -eventName 
> "Setup" -Action {Write-Host "Hugo"}
> 
> [int]$myValue = 200
> 
> $testObject.fireSetupEvent($myValue)
> 
0
Utf
7/1/2010 1:08:29 PM
Reply:

Similar Artilces:

CRM4 reporting errors
I ran a Microsoft Update on my SQL server that has apparently broken the link between CRM4 and SQL Reporting Services. After the update I started receiving this error whenever I tried to run a report: Microsoft CRM Unhandled Error Details: Server Error in '/' Application. One or more data sources is missing credentials I searched and found MS KB946289 that mentions this error when SQL 2005 critical update 934458 is installed. The fix they describe is either repairing or reinstalling the CRM SSRS connector. I tried Repairing and reinstalling the connector but the error did not g...

powershell event
Hi, I am evaluating an architecture using my custom Powershell cmdlets as the middle tier (MT) and using a Native NT service as the backend. Via my powershell cmdlets, UI can query/control the backend and the backend should be able to send notification to UI (UI doesn’t use Polling model for receiving notification). The middle tier will communicate with the backend using COM since they are different language interop. The middle tier will communicate with UI through powershell cmdlets. Therefore, the middle tier will be a Powershell cmdlets dll and an interop assembly at the s...

Error message #32
After opening an e-mail or trying to send an e-mail I receive the following message: Outlook executed an invalid instruction in module <unknown> at 0000:01614ad6. Then details follow. Cannot seem to fix problem. Any suggestions? System is older system with Outlook 2000. ...

Error posting sales invoice
I am posting a sales invoice but, The next message is very awful. There is line of articulos with warnings or errors. Error: The amount of line not this correctly assigned. Any ideas??/ Thanks. I think the reason you aren't getting any replies is because this error doesn't make any sense. This doesn't sound like any error message that I've ever seen. Perhaps if you could recreate this error and give the exact text of the error. patrick mbs dev support -- This posting is provided "AS IS" with no warranties, and confers no rights. "NanyG" <...

Office 2003 SP3 Installation Error 2349
I work in an IT department at our company and I am having a tough time installing on one Windows 2000 PC Service Pack 3 for Office 2003. I am receiving an error message "2349 error" each time. My search on the net has not helped with the issue. I have tried installing it from the original SP3 CD, and I have downloaded the Service Pack from Microsoft and tried installing it locally from a folder. Neither has worked. Please help. Ricardo This issue may occur if you use a DVD or CD-RW drive to install or copy installation files to HD. Use a plain CD-ROM only. Anoth...

Serious Error
I am running XP Professional and Office XP. I set up a Limited Account for my daily use and an Admin Account. Since I have set up the Limited Account, I have started getting a message that my Modem interface has experienced a serious error and must close. Usually this happens after checking email in Outlook with the Limited Account. I have both accounts set up the same with email etc. I am not sure what I did wrong that I keep getting this error. I know that I was in the wrong account when I went to update my modem drivers--was in Limited account instead of Admin account when I expanded ...

Runtime Error
In a previous post, more details about the Runtime Error Alert that I receive was requested for further diagnosing of the problem I am experiencing with Outlook 2003. I have since received the error again... The problem that I have is that, whenever Outlook performs "heavy tasks," such as receiving or sending large or ample messages or processing rules, it abruptly shuts down. This originally was occurring on just one computer, but is now occurring on both of them. I originally was receiving the Runtime Error Alerts when this abrupt shut down occurred, then they wen...

Runtime Error 381 Couldn't set list property, invalid array index #2
I opened a 97 spreadsheet in 2003 and now each time a user form is initialized a runtime error comes up in the following line in the vba code: ComboDeptNum.List = Sheets("Definitions").Range("$J$1:$I$12") It is the same thing if I specify a named range: ComboDeptNum.List = Sheets("Definitions").Range("DeptList") This exact same codes works fine in 97 and the security is set to low in 2003. The number of columns is set propoerly but the list box just won't load values. I'm a newbie to Excel vba but have more experience with Access and never sa...

Server Error in '/' Application. CurrentPrincipal is not authenticated.
Hello, Currently we have installed Business Portal but it is on an Exchange 2003 server and we are getting the following when connecting to the url http://server/sites/BusinessPortal/default.aspx Everything is set to default so if anyone has any suggestions as to how to solve this it would be greatly appreciated! Thanks, Caroline Server Error in '/' Application. -------------------------------------------------------------------------------- CurrentPrincipal is not authenticated. Description: An unhandled exception occurred during the execution of the current web request. Please rev...

An error has occured in the script on this page
I get the following error when trying to print email messages out of Outlook 2000. An error has occurred in the script on this page Line: 297 Char: 26 Error: Expected ')' Code: 0 URL: res://C:\WINNT\system32\shdoclc.dll/preview.dlg Can anyone please help me with a possible solutions to this problem? Thanks Wayne ...

[MSDN documentation error] CEdit::LineLength
Today I was searching information for CEdit::LineLength, and I found what IMHO is a documentation error: CEdit::LineLength http://msdn.microsoft.com/en-us/library/b9a98xd4.aspx In fact, the official documentation says that the return value is the length in *bytes*, but stepping into MFC code implementation (at least on VC2008) shows that the actual return value is the length in TCHAR's (as one would "naturally" think...). I added a community content about that on MSDN site. If this documentation error is confirmed, Joe: you may consider adding that to your "MSDN Docume...

Powershell
Is Powershell necessary for Windows XP to run? I have a basic idea of what it is but is it needed? Boceefus wrote: > Is Powershell necessary for Windows XP to run? I have a basic idea of what > it is but is it needed? No, its not needed, most folks don't have it on their computers, let alone know what it is. John I am using it in my SQL 2008 Express. In fact SQL 2008 won't install unless you have powershell on your system. hth "Boceefus" <Boceefus@discussions.microsoft.com> wrote in message news:7D03AAB2-FFB6-4251-A21C-1B6D7BE68A93@mi...

New Top-Level Site
SBS 2003 R2 with WSS 2.0 Created a new top-level site but we cannot access it due to the following errors: You are not authorized to view this page You do not have permission to view this directory or page using the credentials that you supplied. -------------------------------------------------------------------------------- Please try the following: Contact the Web site administrator if you believe you should be able to view this directory or page. Click the Refresh button to try again with different credentials. HTTP Error 401.1 - Unauthorized: Access is denied du...

if error
i have a macro that reads from a list of data which is autoimaticall generated, some times the error #DIV0! is produced i want to write if cell.value=this error then set variable to zero how do i do this -- Message posted from http://www.ExcelForum.com as always, post your coding efforts for comments -- Don Guillett SalesAid Software donaldb@281.com "robert_woodie >" <<robert_woodie.15j2vv@excelforum-nospam.com> wrote in message news:robert_woodie.15j2vv@excelforum-nospam.com... > i have a macro that reads from a list of data which is autoimatically > generat...

error on eConnect post.
I followed the following block of code, but got this error message. Anyone knows what's the problem? ========== error ============ "eConnect Requester service did not return xml. <eConnectOut><DOCTYPE>Customer</DOCTYPE><OUTPUTTYPE>2</OUTPUTTYPE><INDEX1TO> AARONFIT0001</INDEX1TO><INDEX1FROM> AARONFIT0001</INDEX1FROM><FORLIST>1</FORLIST><ACTION>5</ACTION></eConnectOut>" ========== code ============ Dim serializer As New XmlSerializer(GetType(eConnectType)) Dim eConnect As New eC...

error message #105
I keep getting the following error message whenever I hit Send on a new email: "Object reference not set to an instance of an object" What does this mean? I've tried to google it but get nothing but vb coding answers. JJ What version of Outlook are you using? Did you try running Detect & Repair? (Help menu) -- -Ben- Ben M. Schorr, MVP Roland Schorr & Tower http://www.rolandschorr.com http://www.officeforlawyers.com/outlook.htm "Jeanne" <jc1547@comcast.net> wrote in message news:#deg$$OlIHA.4076@TK2MSFTNGP05.phx.gbl... > I keep getting the...

2 way Vlookup
Hi, I have base data in column A, B, C and D extending from row 2 to row 500. The data in coln A and B together constitute an unique ID. I also have data in Column G and H and these 2 together correspond to the I formed from Column A and B. I want a formula which can return the value from Column D for a particulr ID combination in Column G plus H. Ideally I would concatenate the data in A and B ( = A2&B2) and use this ID for lookup with the concatenated data from G and H (=g2&h2). BUT, in this case due to the naure of the data layout (and some other factors) I cannot afford to creat...

pipelines supported by PowerShell cmdlets
From: http://technet.microsoft.com/en-us/library/dd315282.aspx <# Description The Write-Output cmdlet sends the specified object down the pipeline to the next command. If the command is the last command in the pipeline, the object is displayed in the console. Write-Output sends objects down the primary pipeline, also known as the"output stream" or the "success pipeline." To send error objects down the error pipeline, use Write-Error." #> This mentions two pipelines supported by PowerShell cmdlets 1) primary pipeline, aka "...

FRX
Using GP 7.5, FRx 6.5. When a particular user tries to go into FRx he receives an error. The title of the window is FRx Desktop (Standard) and the error is ActiveX component can't creat object. That's it. When he clicks ok the error changes to FRx will exit. All he can do is click ok. Anyone with ideas would be greatly appreciated. You'll need to install at least SP5 (available on CustomerSource) for Frx 6.5. The SP should help remedy the error. "RM" <rmargan@yahoo.com> wrote in message news:0a8c01c51841$6578e5d0$a501280a@phx.gbl... > Using GP 7.5, FRx...

BLDDEMO: There were errors bulding <project>
I have the following problem when I build CE 6 images: basically, I keep getting the BLDDEMO: There were errors bulding osDesignTS every time I compile an image. However, checking the build.log file in the WINCE600 folder, I found out that the errors are different: - once, I got EDITBIN : fatal error LNK1104: cannot open file 'C:\WINCE600\OSDesigns\osDesignTS\osDesignTS\Wince600\ADVGEODELX_x86\cesysgen\oak\target\x86\debug\exfat.dll' - another time, I got EDITBIN : fatal error LNK1104: cannot open file 'C:\WINCE600\OSDesigns\osDesignTS\osDesignTS\Wince600\ADVGEODELX_x8...

Selection Change Event
I have added an Selection Change Event in my workbook. However, problem is found when I perform copy and paste. The data is "lost" (the "Paste" is in light grey) after I has right clicked another cell. Any solution for this? Thanks in advance for any help! Pat Post the code for the selection change. "pwz" <noreply@abc.com> wrote in message news:eMvi$rfaKHA.5852@TK2MSFTNGP05.phx.gbl... >I have added an Selection Change Event in my workbook. However, problem is >found when I perform copy and paste. The data is "lost&qu...

Error 346: DPM is unable to retrieve the configuration information
Hello, I'm trying to install the DPM agent 2010 on a domain controller in a other domain than the DPM server but with a two-way trust relationship and I get the error 346: DPM is Unable to retrieve the configuration information. The DPM server is Windows 2008 R2 Standard and one where I install the agent is Windows 2003 Standard Service Pack 2 with firewall disabled and the WMI service running. Seems to be a WMI problem because if I try to connect to the WMI console from the DPM server to W2K3 I get the error RPC is not available but locally works fine. If I test by c...

2007 Table Formatting Error
I have a table (created by using the “Format as Table” function). When I add a row to the bottom of the table, some of the cells do not adopt the formatting of the cell above them. How do I overcome this? ...

VBScript & Powershell
I have a 500 line vbscript I wrote years ago that now won't work on Windows 7. The reason is you can't perform a File Open/Browse in vbscript in Windows 7, I(I've tried all the suggestions I've found on the net but none of them work.) I was looking at running a Powershell mini script to do the file open for me then return the selected file back to the vbs script. I've got the Powershell script and it works fine. One twist. I don't want to call another script. I want to send all the Powershell commands at once. The problem I'm seeing is Powershell is ...

error 0x80242006
Got error 0x80242006 when trying to d/l kb978207 given to me by winupd.. xp pro sp2 ie7 Tried to install it manually, but says this is for ie6. title of the update claims it is for ie7 on xp. Any ideas? thanks sue Why are you only attempting to install KB978207 now, nearly two months after it was released? Is this the installer you'd downloaded for manual installation? => http://www.microsoft.com/downloads/details.aspx?FamilyID=3510c7d8-7e8f-479e-b6f9-5745a845664d Why hasn't WinXP SP3 been installed by now? Extended Support for WinXP (x86) SP2 ends...