This question is probably best asked on stackoverflow. That said, take a look at ComDBReleasePort and ComDBClaimPort in the COM Port Database. However, changing COM port assignments programmaticly is not exactly considered a best practice. For example, what happens if two more more concurrent applications are fighting to get the same COM assignment?
Windows dynamically assigns COM port numbers for a good reason. Ideally, your application should be written so that it searches for the specific COM port it needs. This can be done via polling or by using the Windows Management Interface (WMI).
If you're referencing some application that someone else wrote, and you're just stuck with someone else's program design (or lack thereof), then you may want to consider adding a batch script to modify the registry key under HKLM\Hardware\DEVICEMAP\SERIALCOMM
at startup, since the new value(s) are not likely to hold between re-boots.
I don't know how how well this last option would work, but if you're a serious PowerShell or Python user, you could theoretically use either scripting language to directly access the APIs mentioned at the beginning of my response for a one-time COM port re-assignment.