Visual Studio 2022 Community/C++: My Pre-Build PowerShell script runs successfully, but the end result is not achieved.

ondafringe 10 Reputation points
2024-07-06T22:45:14.68+00:00

I trade the market using Sierra Chart (SC) software. I use VS to create a DLL and need to build and deploy the updated DLL to an SC folder without having to shutdown SC in order to release the DLL for writing/updating.

I created a PowerShell (PS) script that uses the UDP port in SC to send a release-DLL directive to SC. When I run that script from the pre-build event within VS, the script runs without error, but SC does not release the DLL, so VS cannot open the DLL for writing.

My PS execution policy is set to: RemoteSigned CurrentUser and AllSigned LocalMachine

======================

This was my Pre-Build Event

PowerShell "Start-Process PowerShell.exe"
PowerShell .\ReleaseDLL.ps1

======================

However, when I manually open a PS window and manually run the same release-DLL script from there, it works as expected -- SC releases the DLL, and VS can successfully deploy the updated DLL. I don't have to open the PS window as Admin, either.

I have a work-around solution where, In the pre-build event, I open a PS window, simulate Ctrl-v to paste the file name into PS, and then simulate pressing the main Enter key to run the script. Then, in the post-build event, I just reverse the process, using a reload-DLL script, and then close the PS window. That works, but it seems like more than should be required.

Any suggestion as to how I can get that script to work when run from the pre-build event?

Thank you.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,196 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 45,701 Reputation points
    2024-07-07T09:43:04.78+00:00

    Since your issue relates to a third-party product I suggest you pursue it with Sierra chart support at https://www.sierrachart.com/SupportBoard.php

    0 comments No comments

  2. Pinaki Ghatak 4,610 Reputation points Microsoft Employee
    2024-07-07T14:45:30.64+00:00

    Hello @ondafringe

    The issue might be related to the context in which the script is being run. When you manually run the script, it’s being executed in your user context. However, when Visual Studio runs the script, it might be using a different context.

    Here are a few suggestions:

    Check the Execution Policy: Make sure that the execution policy allows scripts to be run. You mentioned that your PowerShell execution policy is set to RemoteSigned for CurrentUser and AllSigned for LocalMachine. You might want to try setting it to Unrestricted temporarily to see if that’s the issue.

    Run Visual Studio as Administrator: Sometimes, certain operations require elevated privileges. Try running Visual Studio as an administrator and see if the script executes correctly.

    Specify the Full Path of the Script: In your pre-build event command, you might want to specify the full path of the PowerShell script. Sometimes, relative paths can cause issues.

    1. Add a Pause in the Script: Sometimes, scripts can run too quickly, and the system doesn’t have enough time to release the DLL. Try adding a pause or sleep command in your script to allow the system some time to catch up.

    Check for Errors in the Script: Add some logging to your PowerShell script to see if it’s actually being run. You can write to a text file at various points in your script to see how far it gets.

    This will solve your issue.


    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.