Friday, April 29, 2011

Sharepoint controls in ASP.NET application

Is there a way I can use the sharepoint controls in a ASP.NET web application like any other controls that come out of box for ASP.NET? If yes, what are the pre-requisites I need to install?

Thank you, Harsha

From stackoverflow
  • Yes, you can use SharePoint "controls" in an ASP.NET application, as long as the application is running in SharePoint.

    In other words, the prerequisite is SharePoint.

    Ahmad Farid : how can we do that?
    Harsha : There are so many nice controls in Sharepoint. It would be great if we can use them outside (in ASP.NET application alone)
  • What specific controls are you referring to? Those that you find in SharePoint Designer?

    If you are referring to Web Parts in WSS v3, those that are using the ASP.NET Web Part as the base web part (the recommended approach) may work fine in ASP.NET since the Web Part class inherits from Panel which inherits from Web Control (going from memory here) - all ASP.Net classes. It would just depend on whether the web part has any SharePoint specific code which is highly dependent upon the web part.

  • Host the application in SharePoint's _layouts directory (see this video for more details). Your ASP.NET app will then be "running in SharePoint" and have access to all SharePoint controls.

    Note that some controls don't work unless they are running on an actual SharePoint page.

  • Most controls have internal dependencies on SharePoint (i.e. they use SPContext or SPWeb internally). Also, since they are contained within the Sharepoint Assemblies, you can not just take the .dlls and put them in your app.

    In short: In most cases, it will be better to re-build them using reflector. Which one are you looking at?

Forwarding a Keystroke to another Control in WinForms

I am receiving a keystroke in a Form object's OnKeyDown. When some conditions are met (such as the keystroke is a printable character and not a hotkey) I want to forward the key to a text control on the form and set focus to the text control so the user can continue typing. I am able to decode the character typed using MapVirtualKey but I get only the "unshifted" character (always upper case). Using ToUnicodeEx seems like too much of a PITA.

What is the best way to do this? Isn't there a way to simply forward the Windows message itself?

Can't I intercept the ProcessKeyPreview or some such and forward it to the text control's ProcessKeyPreview? Any ideas along similar lines?

Bump: No answers!

From stackoverflow
  • I did this on a single form with the following

    private void MyFunkyForm_KeyDown(object sender, KeyEventArgs e)
    {
        // to avoid duplicate letters in textbox :-)
        if (textBox2.Focused == false)
        {
    
         // get the char from the Keycode
         char inputChar = (char)e.KeyCode;
         if (char.IsLetterOrDigit(inputChar))
         {
          // if letter or number then add it to the textbox
          textBox2.Text += inputChar;
         }
    
         // set focus
         textBox2.Focus();
         // set cursor to the end of text, no selection
         textBox2.SelectionStart = textBox2.Text.Length;
        }
    }
    

Spawned child exits with state = 127

Hi, I use posix_spawnp to execute different processes and I check the status (with waitpid) to make sure the child was created properly

    int iRet = posix_spawnp(&iPID, zPath, NULL, NULL, argv, environ);    

 if (iRet != 0)
 {
  return false;
 }

 int iState;
 waitpid(static_cast<pid_t>(iPID), &iState, WNOHANG);
 cout << "Wait: PID " << iPID << " | State " << iState << endl;

 if (WIFEXITED(iState)) {
  printf("Child exited with RC=%d\n",WEXITSTATUS(iState));
 }
 else if (WIFSIGNALED(iState)) {
  printf("Child exited via signal %d\n",WTERMSIG(iState));
 }
 else
 {
  printf("Child is NORMAL");
 }

At first this executes properly and I get the following message:

Wait: PID 15911 | State 0 Child exited with RC=0

After executing the same process several times, the child process starts to exit with status 127.

Wait: PID 15947 | State 32512 Child exited with RC=127

After this happens, I could not get the child to spawn again. I enclosed the section of code given above in a for loop but it wouldn't spawn properly. If I restart the parent process, it works for a while but the same problem crops up again after a while.

What am I doing wrong here?

From stackoverflow
  • Check the return code from waitpid() to be sure that it isn't having problems.

    The way the code reads suggests that you are only spawning one child process at a time (otherwise there'd be no need to call waitpid() within the loop). However in that case I wouldn't expect to use WNOHANG.

    Gayan : The waitpid call returns with a value > 0 which means that there's a valid child.
  • Check this link.

    For example:

    EINVAL The value specified by file_actions or attrp is invalid.

    The error codes for the posix_spawn and posix_spawnp subroutines are affected by the following conditions: If this error occurs after the calling process successfully returns from the posix_spawn or posix_spawnp function, the child process might exit with exit status 127.

    It looks as if it might exit with 127 for a whole host of reasons.

    Gayan : I re-wrote the code using fork and execvp to get a more definite grasp of the error and it turned out that the actual error info is: errno = 14 (bad address) Some digging around revealed that this was because I wasn't ending my argument list with a final entry of "NULL". argv = new char[iSize + 1]; argv[iSize] = NULL; fixed the problem.

Is this the command pattern?

Hi,

I have a MVP Gui and now I would like to define certain Actions or Commands (Modify, Save, Close, ...) for certain views.

Is there an easy way to do this? Should I provide Commands for each View?

From stackoverflow
  • The easiest way is to have a factory where all your command objects are instantiated. So if you have a open Job Command all the views would goto the factory and pull out the Open Job Command object, instantiate it, and then execute it. If you need to fix a bug or change the Open Job Command there only one spot you have to do it for all the Views.

    With that being said there will be some commands that will probably be unique to each View. Despite that you may want to still encapsulate those in a command object as you can easily implement Undo/Redo with everything going through command objects.

Winforms to WPF conversion: BeginInvoke to what?

Hi all,

Here's my old code from WinForms:

    private void ValueChanged(double inValue1, double inValue2) {
        //only manual mode for this driver, so that's easy.
        if (ValueLabel.InvokeRequired) {
            ValueLabel.Invoke(new MethodInvoker(delegate {
                ValueLabel.Text = (inValue1* inValue2/ 1000).ToString("f1");
            }
                ));
        }
        else {
            ValueLabel.Text = (inValue1* inValue2/ 1000).ToString("f1");
        }
    }

Is there an easy way to convert this to be WPF friendly? So far, I have:

   private void KVPValueChanged(double inValue1, double inValue2) {
        if (ValueLabel.Dispatcher.Thread == Thread.CurrentThread){
            ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
        } else {
            ValueLabel.Dispatcher.BeginInvoke(delegate {
                ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
            });
        }
    }

But that second 'delegate' call fails. How can I invoke this delegate? I suppose I can go through the whole making a delegate method, making an instance of the delegate method, invoking that particular instance, etc, but I thought the whole point of these anonymous delegates was to avoid that hassle. Plus, my old winforms code has that first implementation all over the place, so I'd really like to avoid having to de-anonymize all my delegates.

Edit: I can try to use the MethodInvoker like I was before, but then the compiler gets confused. MethodInvoker is part of System.Windows.Forms, so using that approach doesn't work. As in:

    private void ValueChanged(double inValue1, double inValue2) {
        if (ValueLabel.Dispatcher.Thread == Thread.CurrentThread) {
            ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
        }
        else {
            ValueLabel.Dispatcher.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate {
                ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
            }));
        }
    }

That use of MethodInvoker is not kosher. Is there a separate implementation of it, or some other way to use the same behavior?

From stackoverflow
  • I think you need to change the signature of the delegate:

    ValueLabel.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate(invalue1, invalue2){
        ValueLabel.Content = ...
    

    Also, look up using the BackgroundWorker component. Not just for wpf but also for the winform async operations.

    mmr : The methodinvoker call doesn't work...
    mmr : der. forgot to include the windows forms library, which is where method invoker is.
    blue_fenix : sry, spoke too soon before looking back into my Pro WPF book for the real answer :)
    mmr : Yes, it does appear that 'ThreadStart' is the magic word here. Thanks!
  • System.Windows.Forms.MethodInvoker is simply a Delegate that take no parameters and returns void. In WPF, you can just replace it with System.Action. There are also other built-in Delegates that accept parameters, return values, or both.

    In your case,

    ValueLabel.Dispatcher.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate {
                ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
            }));
    

    becomes

    ValueLabel.Dispatcher.BeginInvoke(new Action(delegate() {
                ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
            }));
    

run interpret c++?

Is there a way i can run c++ code as interpreted instead of compiled? so i can edit code and write functions on the fly?


Related:

From stackoverflow
  • Take a look at Ch, an embeddable C++ interpreter.

    Ch is an embeddable C/C++ interpreter for cross-platform scripting, shell programming, 2D/3D plotting, numerical computing, and embedded scripting. Ch is a free and user-friendly alternative to C/C++ compilers for beginners to learn C/C++.

  • CINT (readme) certainly has single-stepping. I'm not sure about modification on the fly, though.

  • Ch and CINT (usually as part of the ROOT system) will interpret C++. However, my experience with CINT has not been good: the language support is not complete (particularly where templates are concerned), the execution is much slower, there has been a history of bugs with e.g. variable scope and loop exiting, and (IMO) it's more hassle than it's worth. As a language, C++ is singularly ill-designed for interpreted use.

    If you need to run interpreted code, why not use a modern interpreted language like Python or Ruby? A tool like SWIG can be used to connect them to existing C/C++ libraries if needed.

    MSalters : "ill-designed" suggests it was designed for such use. I'd say "not designed and ill-suited"
  • This doesnt exactly answer your question, but perhaps it will help.

    The MS C++ compiler supports Edit and Continue, which allows you to stop, make changes, recompile & continue without shutting down you program.

  • I saw a presentation on ccons at CUSEC's demo camp back in January. Its aim is to provide an interactive interpreter like python's. It was in its early stages then but impressed me none the less.

  • Try these:

PEVerify MD Error: 0x8013124C

Hi

I get this 'error' when running PEVerify on a custom generated assembly.

[MD](0x8013124C): Error: Method has a duplicate, token=0x06000023. 
                         [token:0x06000021]
[MD](0x8013124C): Error: Method has a duplicate, token=0x06000021. 
                         [token:0x06000023]

Besides this (and 196 others of the exact same error), there are no issues with the metadata and IL. And it works correctly too.

I have been unable to track down where it comes from (as it does not affect assembly in any way).

Google, unfortunately does not reveal much on this error.

Can someone please provide some insight on this 'error' and how it could be caused?

Thanks :)

From stackoverflow
  • It sounds like peverify believes that you have duplicate method rows in the assembly meta data. I read in the comments that you are using Reflection.Emit to generate the assembly. It sounds like it's possible that you are re-using a method definition for generation instead of creating a new one for each method.

  • I solved the problem.

    It is caused by emitting a method with the exact signature of another.

    UPDATE

    This goes for any member. Hence, this will likely have the same MD error when run on obfuscated assemblies.