<p>I've been asked to create a Delphi compatible dll in C++ to do simple 64bit memory management.</p>

<p>The background is that the system in Delphi needs to allocate a lots of chunks of memory that would go well outside 32bit addressable space. The Delphi developer explained to me that he could not allocate memory with the Delphi commands available to him. He says that he can hold a 64bit address, so he just wants to call a function I provide to allocate the memory and return a 64bit pointer to him. Then another function to free up the memory later.</p>

<p>Now, I only have VS 2008 at my disposal so firstly I'm not even sure I can create a Delphi compatible dll in the first place.</p>

<p>Any Delphi experts care to help me out. Maybe there is a way to achieve what he requires without re-inventing the wheel. Other developers must have come across this before in Delphi.</p>

<p>All comments appreciated.</p>

[{'answer_id': 34334, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 3, 'selected': False, 'text': '<p>Only 64 bit processes can address 64 bit memory. A 64 bit process can only load 64 bit dlls and 32 bits processes can only load 32 bits dlls. Delphi\'s compiler can only make 32 bits binaries.</p>\n\n<p>So a 32 bits Delphi exe can not load your 64 bit c++ dll. It could load a 32 bit c++ dll, but then that dll wouldn\'t be able to address the 64 bit memory space. You are kind of stuck with this solution.</p>\n\n<p>Delphi could, with the right compiler options and Windows switches address 3GB of memory without problems. Even more memory could be accessed by a 32 bits process if it uses <a href="http://en.wikipedia.org/wiki/Physical_Address_Extension" rel="nofollow noreferrer">Physical Address Extension</a>. It then needs to swap memory pages in and out of the 32 bits memory through the use of <a href="http://msdn.microsoft.com/en-us/library/aa366527(VS.85).aspx" rel="nofollow noreferrer">Address Windowing Extensions</a>.</p>\n'}, {'answer_id': 35400, 'author': 'Roddy', 'author_id': 1737, 'author_profile': 'https://Stackoverflow.com/users/1737', 'pm_score': 3, 'selected': False, 'text': '<p>Delphi pointers are 32-bit. Period. Your Delphi developer may be able to \'store\' the 64-bit values you want to return to him, but he can\'t access the memory that they point to, so it\'s pretty futile.</p>\n\n<p>Previously, I\'d written:-</p>\n\n<blockquote>\n  <p>A 64-bit version of Delphi is on\n  <a href="http://dn.codegear.com/article/36620" rel="nofollow noreferrer">Codegear/Embarcadero\'s road map</a>\n  for "middle of 2009". Product quality\n  seems to be (at last!) taking\n  precedence over hitting ship dates\n  exactly, so don\'t hold your breath...</p>\n</blockquote>\n\n<p>But, in August 2010, Embarcadero published a <a href="http://edn.embarcadero.com/article/39934" rel="nofollow noreferrer">new roadmap here</a>. This doesn\'t give specific dates, but mentions a <em>64-bit Compiler Preview</em>, with <em>Projected Availability, 1st Half of 2011</em>.</p>\n'}, {'answer_id': 35456, 'author': 'Mat Noguchi', 'author_id': 1799, 'author_profile': 'https://Stackoverflow.com/users/1799', 'pm_score': 2, 'selected': False, 'text': '<p>You might also want to add a way to pin and unpin that 64-bit pointer to a 32-bit memory address. Since this is Delphi, I\'m pretty sure it\'s Windows specific, so you might as well use <a href="http://msdn.microsoft.com/en-us/library/aa366527(VS.85).aspx" rel="nofollow noreferrer">Address Windowing Extensions</a>. That way, you can support allocating, freeing, and pinning and unpinning memory to a 32-bit address range and still take advantage of a 64-bit memory allocation space. Assuming that the user will actually commit the memory such that it fits in the 32-bit virtual address space.</p>\n'}, {'answer_id': 58175, 'author': 'Jim McKeeth', 'author_id': 255, 'author_profile': 'https://Stackoverflow.com/users/255', 'pm_score': 3, 'selected': False, 'text': '<p>You might take a look at <a href="http://www.freepascal.org/" rel="nofollow noreferrer">Free Pascal</a> as it includes a 64 bit version and is mostly Delphi compatible syntax.</p>\n'}, {'answer_id': 6148685, 'author': 'Arnaud Bouchez', 'author_id': 458259, 'author_profile': 'https://Stackoverflow.com/users/458259', 'pm_score': 2, 'selected': False, 'text': '<p>In order to allocate memory shared by multiple process, you should use a memory mapped file.</p>\n\n<p>The code available at <a href="http://www.delphifaq.com/faq/delphi_windows_API/f348.shtml" rel="nofollow">http://www.delphifaq.com/faq/delphi_windows_API/f348.shtml</a> can be used to communicate between a 32 bit and a 64 bit process.</p>\n\n<p>Here are the steps:</p>\n\n<ul>\n<li>Create a memory mapped file, either on disk, either on memory;</li>\n<li>Create a mutex to notify file change;</li>\n<li>One end write some data to the memory mapped file;</li>\n<li>Then it flags the mutex;</li>\n<li>Other end receive the mutex notification;</li>\n<li>Then it reads the data from the memory mapped file.</li>\n</ul>\n\n<p>It\'s up to you to create a custom binary layout in the memory mapped file, in order to share any data.</p>\n\n<p>By design, memory mapped files are fast (it\'s a kernel-level / x86 CPU feature), and can handle huge memory (up to 1 GB for a 32 bit process, from my experiment).</p>\n\n<p>This kind of communication is used by <a href="http://cc.embarcadero.com/Author/802978" rel="nofollow">http://cc.embarcadero.com/Author/802978</a> to call any 64 bit dll from a 32 bit Delphi program.</p>\n'}]
['https://Stackoverflow.com/questions/34294', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3585/']